Search code examples
joomlajoomla2.5joomla-component

Joomla 2.5 custom component: filter entries


In a custom component, in the site view, I display a list of countries, each as a link to another page, displaying persons living in that country.

This is a link:

index.php?option=com_example&view=persons&country=1&Itemid=131

What's missing:
When the persons-page is opened, all persons are listed.

What I'm looking for:
I'd like to show only persons with country as in the link, 1 in the example above.

I tried to add this condition in the model-files of persons, but failed miserably.

+++ EDIT ++++

Thanks to the accepted answer, I was able to accomplish what I needed. Unfortunately, this seems to produce side-effects:

Fatal error: Call to a member function getPagesCounter() on a non-object 
in .../view/persons/tmpl/default.php` (...)

The code throwing that error is

<?php echo $this->pagination->getPagesCounter(); ?>

When commenting out that line, the same erroer will occur with this code:

<?php echo $this->pagination->getPagesLinks(); ?>

How did that happen, and what can I do? Tried to track down that problem, but didn't know where to start.

+++ EDIT +++

Wasnm't able to solve that issue yet. Did a var_dump($this->pagination);, this is the output:

array(1) {
  [0]=>
  object(stdClass)#150 (20) {
    ["id"]=>
    string(1) "6"
    ["name"]=>
    string(11) "Fleur Leroc"
    ["country"]=>
    string(1) "2"
    (...)    
    ["ordering"]=>
    string(1) "6"
    ["state"]=>
    string(1) "1"
    ["checked_out"]=>
    string(3) "615"
    ["checked_out_time"]=>
    string(19) "2013-10-10 10:53:14"
    ["created_by"]=>
    string(10) "Super User"
    ["editor"]=>
    string(10) "Super User"
    ["countriestrainers_country_828045"]=>
    string(6) "France"
    ["countriestrainers_flag_828045"]=>
    string(28) "images/trainers/flags/fr.gif"
  }
}

So the object does exist, doesn't it?


Solution

  • You were close editing the model files. In your Persons model (ExampleModelPersons) you need to make sure you have the following elements:

    Whitelist the filter name:

    <?php
    public function __construct($config = array())
        {
            if (empty($config['filter_fields'])) {
                $config['filter_fields'] = array(
                    'country',
                    // other not standard filters
                );
            }
            parent::__construct($config);
        }
    ?>
    

    Autopopulate the state filter:

    <?php
    protected function populateState($ordering = null, $direction = null)
    {
    
        $country = $this->getUserStateFromRequest($this->context.'.filter.country', 'country', '',  null, false);
        $this->setState('filter.country', (int) $country);
            // ....Other states
    {
    ?>
    

    Store id for the context:

    <?php
    protected function getStoreId($id = '')
    {
        $id .= ':'.$this->getState('filter.country');
        // Other states
    }
    ?>
    

    And the most important one, the database query

    <?php
    protected function getListQuery()
    {
        // ... Other parts of the querty
        if ($country = $this->getState('filter.country'))
            $query->where("country = ". (int) $country);
    }
    ?>
    

    If you don't need saving the state in user's session this can be easily stripped into two liner in the database query.

    <?php
        // ... Other parts of the querty
        if ($country = $app->input->getInt('country'))
            $query->where("country = ". (int) $country);
    ?>