0
votes

I've built a custom component in Joomla 3.6. The component itself is working fine, but I'm having trouble accessing user state variables from custom fields inside the component and from a separate module. When I try, I get nothing returned.

Here's my code from populatestate() in the model:

$app = JFactory::getApplication();
$filter_product_group_category = $app->getUserStateFromRequest('filter.product_group_category', 'filter[product_group_category]', '', 'string');
$this->setState('filter.product_group_category', $filter_product_group_category);

$filter_product_group_type = $app->getUserStateFromRequest('filter.product_group_type', 'filter[product_group_type]', '', 'string');
$this->setState('filter.product_group_type', $filter_product_group_type);

$filter_search = $app->getUserStateFromRequest('filter.search', 'filter[search]', '', 'string');
$this->setState('filter.search', $filter_search);

Here's the code I'm using from within the custom field and the module:

$mainframe =JFactory::getApplication();
$filter_product_group_category = $mainframe->getUserState("filter.product_group_category");
$filter_product_group_type = $mainframe->getUserState("filter.product_group_type");
$filter_search = $mainframe->getUserState("filter.search");

echo $filter_product_group_category;
echo $filter_product_group_type;
echo $filter_search;

I'm obviously doing something wrong, but I've exhausted my knowledge and spent hours of Google research without getting any closer. Any help appreciated!

2

2 Answers

0
votes

In the module file i have something like this:

JModelLegacy::addIncludePath(JPATH_SITE . '/components/com_aoeodb/models', 'AoeoDBModel');

$view                   = ModAoeoDBHelper::getFilterView();

$model                  = JModelLegacy::getInstance(ucfirst($view), 'AoeoDBModel', array('ignore_request' => true));

Then in the helper:

$form       = $model->getForm();        
$filters    = $form->getFieldset();

    foreach ($filters as $field)
    {

        if($field->getAttribute('name') <> 'search') 
        {

            $form->setValue($field->getAttribute('name'), 'filter', $model->getState('com_aoeodb.'. $view . '.filter.' . $field->getAttribute('name')));

        }               

    }

    return $filters;

It gets the modal values based on the form I'm using in the module and sets the field value.

0
votes

The component state is stored in post and not in session so if you want to get values so should get them from model. As in Mind gem example.

If you want to store values in session, so in your model you should add values in user state :

$app = JFactory::getApplication();
$filter_product_group_category = $app->getUserStateFromRequest('filter.product_group_category', 'filter[product_group_category]', '', 'string');
$app->setUserState('filter.product_group_category', $filter_product_group_category);

Hope this help.