3

Based on some internal conditions, I need to filter a list and have the "Search Tools" engaged showing the selected item in the filter list. The net effect that we want is the same as appending the filter to the url like this http:...&Itemid=161&filter[parent_id]=29, which does exactly what I want.

According to Setting and retrieving user state variables:

There are two ways to set user state variables. The first is fairly intuitive, and is done using the JApplication::setUserState method. The second is a little less obvious, and uses the JApplication::getUserStateFromRequest method. There are also two ways to retrieve user state variables. The first is using the JApplication::getUserState method, and the second is to use the JApplication::getUserStateFromRequest method. JApplication::getUserStateFromRequest can be used to both store and retrieve user state variables.

This is all fine for retrieving the information, but I'm unclear about the method and best place to SET a state variable to accomplish what the above URL does so that the value is saved where all the parts and pieces expected to find it along the way.

I've tried variations of this code in a number of places, and can usually get the output filtered, but unable to have the Search Tools displayed, so am apparently missing the part that sets up the values that ->getUserStateFromRequest and other subsequent functions use.

// Load the filter state.
$search = $app->getUserStateFromRequest($this->context.'.filter.search', 'filter_search');
$this->setState('filter.search', preg_replace('/\s+/',' ', $search));

The code has a loop like below - how do I "set" whatever $app->getUserStateFromRequest is going to "get" as the filters to loop through?

if ($filters = $app->getUserStateFromRequest($this->context . '.filter', 'filter', array(), 'array')) {
    foreach ($filters as $name => $value) {
        $this->setState('filter.' . $name, $value);
    }
}
2

Inside the display method of your view class put this line before parent::display($tpl);:

$this->get('State')->filter = array('Foo'=>'Bar');

I always use this line to filter featured items in my components and it works:

$this->get('State')->{'filter.featured'} = 1;

Hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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