Add filters to Joomla3-component (Backend)

4,067 views
Skip to first unread message

Timotheus Titus

unread,
Feb 27, 2014, 5:05:33 AM2/27/14
to joomla-de...@googlegroups.com
Hello :-)

I want to include filters in the backend of my component.
Well, the tutorials I have are for Joomla 2.5 - I'm using Joomla 3.2.2.

There seems to be a possibility to add filters with JHtmlSidebar::addFilter . I guess I have to do this in my view.html.php?

Like this:


JHtmlSidebar::addFilter(
            JText::_('COM_BESTIA_SELECT_ITEM'),
            'filter_item_id',
            JHtml::_('select.options', ItemsHelper::getItemOptions(), 'value', 'text', $this->state->get('filter.item_id'))
        );



Well, if I include this to my view.html.php, I get a Filter-Field with the content of the options, which are returned by the helper-method.

But how can I include a fulltext-search like here: http://picload.org/view/ldpgwli/bildschirmfoto20.png.html ?


Timotheus Titus

unread,
Feb 27, 2014, 5:16:24 AM2/27/14
to joomla-de...@googlegroups.com
Hey there :-)

in com_banners etc. they use


        <?php
        // Search tools bar
        echo JLayoutHelper::render('joomla.searchtools.default', array('view' => $this));
        ?>

But if I try to use this, I'm getting an PHP-Error:

Fatal error: Call to a member function getGroup() on a non-object in /var/www/vhosts/bestia.web/httpdocs/layouts/joomla/searchtools/default/bar.php on line 26


What am I doing wrong?

Cliff Ford

unread,
Feb 27, 2014, 6:15:35 AM2/27/14
to joomla-de...@googlegroups.com
I think it is time you splashed out on 3.2 documentation:

http://www.packtpub.com/learning-joomla-3-extension-development/book

I bought the electronic version and was impressed with how clear it was. I learned stuff that I should have really known.

Cliff
--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-gene...@googlegroups.com.
To post to this group, send an email to joomla-de...@googlegroups.com.
Visit this group at http://groups.google.com/group/joomla-dev-general.
For more options, visit https://groups.google.com/groups/opt_out.

Adam Rifat

unread,
Feb 27, 2014, 8:17:47 AM2/27/14
to joomla-de...@googlegroups.com
The error is because you're not passing in a valid form object to the layout.

Line 26 of layouts.joomla.searchtools.bar.php:

$filters = $data['view']->filterForm->getGroup('filter');

Most likely $this that you pass into the layout does have a filterForm property. Make sense?

Farahmand Moslemi

unread,
Jun 5, 2014, 3:48:41 PM6/5/14
to joomla-de...@googlegroups.com
Hi,

Don't forget to put an xml file into

[administrator]/components/com_YOURCOMPONENT/models/forms/

which defines all the filters. The file should have this format: "filter_YOURMODEL.xml".

Try to find out what other core components like com_content do.

Hope it helps.

TownWebsites

unread,
Nov 16, 2014, 1:02:02 PM11/16/14
to joomla-de...@googlegroups.com
Another issue here is that if you are updating a component that is using JModelListLegacy, the legacy class does not include the new filter functionality, so the filter xml is not loaded and the function not present.

Charlie Heath
Town Websites

David Ascher

unread,
May 13, 2015, 5:56:56 PM5/13/15
to joomla-de...@googlegroups.com
It looks easy to add a filter for a field being equal to some value where the possible values come from a field in a db table. It is simply a matter of using com_content as a template and modifying for your own use.

What I want to do is have a filter where the rows have a value equal either to 0 or 1.  It seems a bit overly complicated to create another table that contains only the values 0 and 1 (and some text field to go with each value) in order to follow the model of com_content. 

I have not been able to find appropriate sample code - just a lot of confusing and seemingly contradictory documentation that tells some little bit of the story. 

Where can I find ALL the things I need to do to implement this very simple filter - which I may want to extend a little - maybe to 0, 1, 2, or 3 - in the future. OR MUST I create a new table and populate it and go through creating a model and a view for this attribute that does not require the flexibility of something like a category (where I add new ones all the time). Just a static list of options.

Viper

unread,
May 13, 2015, 7:19:48 PM5/13/15
to joomla-de...@googlegroups.com
Custom input field? And you can populate the list(or something else) with sql field type. Or just write you own sql field type.

David Ascher

unread,
May 13, 2015, 9:53:10 PM5/13/15
to joomla-de...@googlegroups.com

The samples I CAN find describe using sql field type. I have already done that for a couple of filters. However, for this new field I want to a filter to select all the rows where the value in a specific field is either 0 or 1. I want the filter to display text - 'Music' for '0' and 'Dance' for '1'. I know I could create a new table which has only 
0 Music
1 Dance

and follow the same process I did for the other filters, but it seems that for something this simple there must be a much simpler way to set up the filter. No?

thanks

Viper

unread,
May 14, 2015, 2:04:05 AM5/14/15
to joomla-de...@googlegroups.com
If your values not hardcoded, it's better to use relation table.

Hannes Papenberg

unread,
May 14, 2015, 6:29:51 AM5/14/15
to joomla-de...@googlegroups.com
Sorry, but I don't understand what you are trying to do. Are we talking
about a dropdown filter like in the backend list views? In that case
simply add
<field name="somename" type="list" label="SOMELABEL"><option
value="0">Value 0</option><option value="1">Value 1</option></field>
into your JForm filter defintion.

Hannes
> --
> You received this message because you are subscribed to the Google
> Groups "Joomla! General Development" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to joomla-dev-gene...@googlegroups.com
> <mailto:joomla-dev-gene...@googlegroups.com>.
> To post to this group, send email to
> joomla-de...@googlegroups.com
> <mailto:joomla-de...@googlegroups.com>.
> For more options, visit https://groups.google.com/d/optout.

Mathew Lenning

unread,
May 14, 2015, 8:14:59 AM5/14/15
to joomla-de...@googlegroups.com
Hannes answer is spot on, but I'd like to add a simple pro tip for you and anyone reading this.

0 and 1 are great numbers, but encoding your DB with them is an anti-pattern which will make your life harder for no good reason.

Instead change I would change that field to a varchar and use the actual meaning instead (I.E music, dance) then set your folders for those values.

This not only makes your DB human readable, but it will save you the headache during translation or display of the value.

Lastly it makes your code self documenting

If($var == 0){...}. VS if($var == 'dance'){...}

Hope this is useful =^D Happy Joomla!ng


Happy Joomla!ng

Dmitry Rekun aka b2z

unread,
May 14, 2015, 8:34:57 AM5/14/15
to joomla-de...@googlegroups.com

Mathew Lenning

unread,
May 14, 2015, 8:57:48 AM5/14/15
to joomla-de...@googlegroups.com
Nice link and I agree completely. If the numeric value actually has a meaning, but the 0 & 1 we're talking about here are a form of double encoding which is always bad.

BTW another benefit of using varchar here is you can get the values for the filter using the a distinct query on the table being filtered (I.E. No hard coded values)

So adding a new value to the filter is as easy as adding a record with a distinct value in the column in question.

David Ascher

unread,
May 15, 2015, 1:07:56 PM5/15/15
to joomla-de...@googlegroups.com
Yes, this is what I wanted to do - 

does this mean that I need to add the field to the xml file for the specific form (actually I do not have any xml file for this for which just lists 'music' and 'dance' 'events' - similar to how the articles backend form lists articles) under model/forms OR somehow as a fieldname.php file for a 'custom field' under model/fields ...  It would be great if I could find a 'template' for how to do this amongst the standard components, but I have not yet seen it.

thanks for your interest and help



David Ascher

unread,
May 15, 2015, 3:19:39 PM5/15/15
to joomla-de...@googlegroups.com
good points - but it does seem like a lot of work to set this up for just a simple 'music' or 'dance' choice.

Mathew Lenning

unread,
May 16, 2015, 7:12:55 AM5/16/15
to joomla-de...@googlegroups.com
I'm pulling this from memory, so forgive me if there are any missing parts.

First in your model create a function to get the list

Public function getActivitesList()
{
$dbo = JFactory::getDbo();
$query = $dbo->getQuery(true);

$query->select('DISTRICT(columnName) AS value, columnName AS text')->from('#__table_name')->where('PkName != 0');

$dbo->setQuery($query);
Return $dbo->loadAssoc();
}

Then in your views getFilters method call this function to get the list.

My filters auto populate, so I don't really remember code that builds the filters anymore.

Hope this helps

David Ascher

unread,
May 17, 2015, 8:02:56 AM5/17/15
to joomla-de...@googlegroups.com
Thanks for trying but I think you forgot the point of my inquiry. If had I wanted to have another table which contained the 'activities' (as in your sample) then I would not have posted a question here. I have been trying to avoid creating a new table containing just the two values for "music" and "dance" and the also the necessity of creating a model file to take care of the getOptions method. All that object oriented stuff is powerful but seemed to me to be massively overly complicated for something that has only two possible values.

For anybody else faced with a similar problem here is the solution I came up with.

I poked around more and found the code that sets up the options for the dropdown that determines the number of items to display on a page ( getLimitBox). Using that as a template I was able to get the filter to work using 

 
<select name="filter_music_dance" class="inputbox" onchange="this.form.submit()" style="width: 75px;">
               
<option value="-1">-Type-</option>
   
<?php
             
# build the select
             
# create an array of values


                $music_dance_options
= array();
                $music_dance_options
[] = JHtml::_('select.option', '0', "Music");
                $music_dance_options
[] = JHtml::_('select.option', '1', "Dance");
                echo
JHtml::_('select.options', $music_dance_options, 'value', 'text', $this->state->get('filter.music_dance'));?>
               
</select>


in the template file and 

                // Filter calendar type - music_dance
                $music_dance
= $db->escape($this->getState('filter.music_dance'));
               
if ($music_dance != -1) {
                        $query
->where('(a.gigcal_music_dance='.$music_dance.')');
               
}

in the already existing models file

I know this is not as elegant as it could be. I hope that by posting the solution I came up with, what I was attempting to accomplish will be clearer to the Joomla gods - perhaps eliciting some suggestions for improvement.


Viper

unread,
May 17, 2015, 9:36:37 AM5/17/15
to joomla-de...@googlegroups.com
So you question was about "how to add filter field with values without xml"? :)

Mathew Lenning

unread,
May 18, 2015, 7:50:33 AM5/18/15
to joomla-de...@googlegroups.com
Actually I think I didn't make myself clear. The table you're querying is the table that you're filtering. Not a separate table. And the model that your adding the function to is the model you already have for that table.

But know worries there are a million ways to do this, so choose which ever way works for you. =^D happy Joomla!ng

David Ascher

unread,
May 18, 2015, 10:01:57 AM5/18/15
to joomla-de...@googlegroups.com

I didn't understand that from your post.  That would work, however, only if I had used 'music' and 'dance' as values in the table instead of the 0 and 1 had already used (right?)... a choice that I can see may come back to haunt me later for various reasons.

Thanks for the clarification and for your efforts.

Mathew Lenning

unread,
May 19, 2015, 11:14:22 AM5/19/15
to joomla-de...@googlegroups.com
Yes, it would only work if you're using a human readable value. But fortunately fixing that is easy in SQL.

First you need to change the column to a varchar.

$dbo = JFactory::getDbo();
$dbo->setQuery('ALTER TABLE #__table_name CHANGE columnName columnName VARCHAR (40)');
$dbo->execute();

Then just update the 0 & 1 to the human readable values

$query = $dbo->getQuery(true);
$query->update('#__table_name)->set('columnName = '.$dbo->quote('Music'))->where('columnName = 0'));
$dbo->setQuery($query)->execute();

$query->clear(array('set','where');
$query->('columnName = '.$dbo->quote('Dance'))->where('columnName = 1'));
$dbo->setQuery($query)->execute();

And you're good to go. I'm on my phone right now, so there might be some errors in the above code, but it should get you mostly there.

=^D happy Joomla!ng

David Ascher

unread,
May 19, 2015, 7:41:52 PM5/19/15
to joomla-de...@googlegroups.com

Thank you very much for that.  I was thinking that I should do the conversion, but my SQL skills are very limited and it seemed like another big efforts.  Using your code as a starting point \, I am pretty sure I can handle it.

Thank you again.

--
You received this message because you are subscribed to a topic in the Google Groups "Joomla! General Development" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/joomla-dev-general/Rs9ekZ_JCN0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to joomla-dev-gene...@googlegroups.com.
To post to this group, send an email to joomla-de...@googlegroups.com.

Mathew Lenning

unread,
May 20, 2015, 9:30:32 AM5/20/15
to joomla-de...@googlegroups.com
Glad I could help =^D 

David Ascher

unread,
May 23, 2015, 5:08:18 PM5/23/15
to joomla-de...@googlegroups.com

I decided that I should do this "the right way" and followed your sample code (removing the obvious typos) and created a models/fields/eventType.php  file (I decided to make the name of this field more generic - looking toward the future) that is identical to another field I for which I already have a working filter. I modified the appropriate lines 
class JFormFieldFweventType extends JFormFieldList

and
        protected $type = 'fweventType';

and I created the query for this within the getOptions method

                $query->select('eventType As value, eventType As text');
                $query->from('#__fwgigcal_gigs AS a');
                $query->order('a.eventType');
                $query->where('published = 1');

However, I am getting the message 
Fatal error: Call to a member function getOptions() on a non-object in /home/folkwork/public_html/folkworks/test1/administrator/components/com_fwgigcal/views/gigs/tmpl/default.php on line 30

this is occurring in the template file where I have 
$eventTypes = JFormHelper::loadFieldType('fweventType', false);
$eventTypeOptions = $eventType->getOptions();

If I check on the type of the $eventTypes variable I see that is is NULL. That seems to me to mean that I need to do something else somewhere (some additional file?) to make this $eventTypes variable become an object as a result of the JFormHelper::loadFieldType('fweventType', false) method invocation. I have no clue where that might be. 

Any idea what I might have missed? 

Viper

unread,
May 24, 2015, 2:08:38 AM5/24/15
to joomla-de...@googlegroups.com
You missed manuals.

Add in template
JFormHelper::addFieldPath(JPATH_COMPONENT . '/models/fields');
$eventTypes = JFormHelper::loadFieldType('fweventType');
...
echo $eventTypes->getInput('params');

Be aware that all methods called from this class should be public.

David Ascher

unread,
May 24, 2015, 7:53:20 PM5/24/15
to joomla-de...@googlegroups.com
The line 

JFormHelper::addFieldPath(JPATH_COMPONENT . '/models/fields');

was already in my template file - as i said, I have two other filters that work just fine and the line was put in to make them work.

I also already have 
$eventTypes = JFormHelper::loadFieldType('fweventType', false);



$eventTypes = JFormHelper::loadFieldType('fweventType');

I also have 

$eventTypesOptions =  $eventType->getOptions();

which I would expect to be populating the options - but since the JFormHelper::loadFieldType('fweventType') seems NOT to be creating an instance of the 'fweventTypes' object, I wouldn't expect the method to work. As I wrote, when I check on the variable type for $eventTypes it is NULL...

If the object were being created I would suspect that perhaps the method getOptions is not public - but the problem happens before the method is invoked - and the method IS declared public.

So something is preventing the creation of a new 'fweventTypes' object... but I don't think you've found what the problem is.

The only thing I can think of is that, perhaps, I should not have a field named fweventType - with a capital "T". Maybe it would work with a lowercase "t".

I'll give that a shot.

 

David Ascher

unread,
May 24, 2015, 8:01:15 PM5/24/15
to joomla-de...@googlegroups.com
naahh.. THat would have been too easy. I changed the name of the fields file in models/fields from eventType.php to eventtype.php and also change the class declaration from

class JFormFieldFweventType extends JFormFieldList
to
class JFormFieldFweventtype extends JFormFieldList

and the field declaration from

        protected $type = 'fweventType';
to
        protected $type = 'fweventtype';

Nothing changed. Still get a NULL value for the line

$eventTypes = JFormHelper::loadFieldType('fweventtype', false);

using echo "eventTypeOptions = ". gettype($eventTypeOptions);

I am almost sure this is due to something trivial, but I just do not see it.

Hannes Papenberg

unread,
May 25, 2015, 8:35:58 AM5/25/15
to joomla-de...@googlegroups.com
How should Joomla know that the class in the file "eventType.php" is
named JFormFieldFweventType? Name the class JFormFieldEventtype.
> --
> You received this message because you are subscribed to the Google
> Groups "Joomla! General Development" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to joomla-dev-gene...@googlegroups.com
> <mailto:joomla-dev-gene...@googlegroups.com>.
> To post to this group, send email to
> joomla-de...@googlegroups.com
> <mailto:joomla-de...@googlegroups.com>.

David Ascher

unread,
May 25, 2015, 4:20:29 PM5/25/15
to joomla-de...@googlegroups.com
I thought you had found the problem - until, instead of changing the class name, I changed the file name to "fweventtype.php" and left everything else as it was -
in the template file using "fweventtype" as the field name 
 
$eventTypes = JFormHelper::loadFieldType('fweventtype', false);

and in the now renamed "fweventtype.php" file declaring (as before) 
class JFormFieldFweventtype extends JFormFieldList

and

protected $type = 'fweventtype';

Unfortunately, now that the file name was consistent with the class and type definitions, I got the same unsatisfactory results.

HOWEVER, as I looked more closely at the code in the template I discovered a few dumb typos that were causing the problem (as usual). I am now 'in business'.

Thank you Hannes, Viper, and Mathew for your persistence. I might never have found the underlying error(s) if not for your suggestions.

All this filtering stuff is finally starting to make some sense to me. Previously is was magic spells.
Reply all
Reply to author
Forward
0 new messages