0

In my custom component...

  • I display a list of countries as links and an extra-link called "dogs"
  • once a country is clicked, a list of persons in that country is displayed (view: persons), by adding a filter country in the model.
  • if "dogs" is clicked, a list of persons owning a dog is displayed (view: persons), regardless of the country they live in. This is done by adding a filter dog in the model.

Task
in persons-view, I want to display a title depending if country or dog is set as filter, e.g. "Austria" or "Persons with Dogs".

What I tried...
reading the active filter from my view-object returns both filters always as 1, even if it isn't set:

["filter.country"]=>int(1)
["filter.dog"]=>int(1)
["list.limit"]=>string(2) "20"
(...)

my model-file (snippet):

protected function populateState($ordering = null, $direction = null) {
    $app = JFactory::getApplication();
    $country = $this->getUserStateFromRequest($this->context.'.filter.country', 'country', '',  null, false);
    $this->setState('filter.country', (int) $country);
    $dog = $this->getUserStateFromRequest($this->context.'.filter.dog', 'dog', '',  null, false);
    $this->setState('filter.dog', (int) $dog);
    (...)
    parent::populateState($ordering, $direction);
}

EDIT:

public function __construct($config = array()) {
    if (empty($config['filter_fields'])) {
        $config['filter_fields'] = array(
            'country',
            'dog'
        );
    }
    parent::__construct($config);
}

view.html.php:

public function display($tpl = null) {
    $app = JFactory::getApplication();     
    $this->state = $this->get('State');
    (...) 

I'm lost - is it nonsense to read the information from the state, or are my filters set incorrectly?

michi
  • 6,565
  • 4
  • 33
  • 56
  • Are these filters declared in Model's constructor? – WooDzu Oct 17 '13 at 21:48
  • @WooDzu: Yes, see my edit. BTW hello again ;-) – michi Oct 17 '13 at 22:16
  • It should work there's nothing unusual in your code. Perhaps there's something missing in your View. Since the state is not populated automatically it needs to be fired with $state = $model->getState() so then you can get a filter with $dog = $state->get('dog') – WooDzu Oct 17 '13 at 22:29
  • @WooDzu: I call `$this->get('State')` in the `function display`. When I `echo $this->state->get('dog')` in `default.php`, I get no output at all :-( – michi Oct 17 '13 at 22:41
  • @WooDzu: `$this->state->get('filter.dog')` is working like a charm, pls write an answer so I can accept it. – michi Oct 17 '13 at 23:19
  • Good point. There's no chance $state->get('dog') would work. Well done for figuring this out – WooDzu Oct 17 '13 at 23:37
  • @WooDzu: Unfortunately, this works only at the first time going from countries to persons. On the second time, `filter.contry` and `filter.dog` will keep their values, regardless of the parameters in the url, e.g. in the url `dog=1`, `country` is not in the url, `var_dump($this->state)` will yield `filter-country = 2`and `filter.dog = 1`... I don't get it. Any clue? – michi Oct 20 '13 at 23:27

0 Answers0