0

I have lots of custom filters in my J3 front-end components and I'm trying to convert to J4, but always get

0 Call to a member function getcatid() on bool

com_aaprueba/src/Field/filtercatid.php


namespace Sfi\Component\Aaprueba\Site\Field;

defined('_JEXEC') or die();

use Joomla\CMS\Form\FormHelper;
use Joomla\CMS\Form\FormField;

class filtercatid extends FormField 
{
    protected $type = 'filtercatid';
    
    public function getCatid($value) 
    {
        ?>
            <select 
                id="filter_catid" 
                name="filter_catid"
                class="form-control js-example-basic-single"
                onchange="this.form.submit();"
                >
                <option value="uno">Uno</option>
                <option value="dos">Dos</option>
            </select>        
        <?php
    }
}

src/Model/EspaciosModel.php


protected function populateState($ordering = null, $direction = null)   
{
    $catid = $this->getUserStateFromRequest($this->context.'.filter.catid', 'filter_catid');
    $this->setState('filter.catid', $catid); 
        
    parent::populateState('a.espacio', 'ASC');
}

protected function getListQuery()
{
...

src/View/Espacios/HtmlView.php


namespace Sfi\Component\Aaprueba\Site\View\Espacios;

\defined('_JEXEC') or die;

use Joomla\CMS\Factory;
use Joomla\CMS\MVC\View\HtmlView as BaseHtmlView;
use Joomla\CMS\Helper\ContentHelper;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\MVC\View\GenericDataException;

class HtmlView extends BaseHtmlView
{
    protected $state;
    protected $items;
    protected $pagination;

    public function display($tpl = null)
    {
        $this->state        = $this->get('State');
        $this->items        = $this->get('Items');
        $this->pagination   = $this->get('Pagination');

        if (\count($errors = $this->get('Errors')))
        {
            throw new GenericDataException(implode("\n", $errors), 500);
        }

        return parent::display($tpl);
    }
}

tmpl/espacios/default.php


use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Router\Route;
use Joomla\CMS\Uri\Uri;
use Joomla\CMS\Layout\FileLayout;
use Joomla\CMS\Form\FormHelper;
?>
...
<form action="<?= Uri::current() ?>" method="post" name="adminForm" id="adminForm">
    < ?php 
        $selectCatid = FormHelper::loadFieldType('filtercatid', false);
        $selectCatid->getcatid($this->escape($this->state->get('filter.catid')));
    ?> 
...
</form>

Please, is there some information about how create custom filters in Joomla 4 without using a xml form? Thanks, JL

0

1 Answer 1

1

You don't need to use a form field class if you don't intend to use it with Form API. There are many ways a loose HTML element can be rendered. You could make it into a HTML service, use one of the provided HTML services (e.g. Joomla\CMS\HTML\HTMLHelper::_('select.genericlist')), put it in a plain helper class or in a layout. And if it's not meant to reusable, you can just hardcode it in the tmpl/espacios/default.php layout.

That said, if you still want it to be loadable as a field, you need to rename the class from filtercatid to FiltercatidField. The class file needs to be renamed the same way. And before calling FormHelper::loadFieldType(), call this to add the namespace for lookup:

FormHelper::addFieldPrefix('Sfi\\Component\\Aaprueba\\Site\\Field')
1
  • Thank you for edit and your complete answer. I think hardcode in the layout option and loading as a filed are the best solutions for me. I have followed your instructions and now it works fine.
    – Peli
    Commented Dec 7, 2022 at 9:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.