Forum
Filtering category based on dropdown selection
Hi there,
I'm trying to filter categories based on the value of a dropdown. If user select yes
in the dropdown, the category will be shown on the home page. My template looks like this:
<perch:before><ul class="column column--by-four"></perch:before>
<li class="column__item">
<perch:category id="catTitle" type="smarttext" label="Title" required="true" />
<perch:category id="catSlug" type="slug" for="catTitle" suppress="true" />
<perch:category id="home_visible" type="select" label="Visible on home page" options="Yes, No" allowempty="false" required="true" />
</li>
<perch:after></ul></perch:after>
I output the categories like so:
perch_categories(array(
'set' => 'clients',
'template' => 'clients.html',
'filter' => 'home_visible',
'match' => 'eq',
'value' => 'Yes'
));
Somehow, I get no result or a empty array when I skip-template. If I remove the filter, match and value, here's the output I get:
Array
(
[0] => Array
(
[home_visible] => Yes
[catID] => 4
[setID] => 2
[catParentID] => 0
[catTitle] => Something
[catSlug] => something
[catPath] => clients/something/
[catDisplayPath] => Something
[catOrder] => 1
[catTreePosition] => 002-001
[catDepth] => 1
[...]
)
Any idea? Weird part, if I filter based on catTitle, it does work.
Cheers,
Hmm, interesting!
Could you add debug to the page and let me know what it outputs?
Ha, looks like the debug has something interesting:
Invalid query: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'home_visible' in 'where clause'
If that helps: the same issue occurred when I tried to use a checkbox.
I think it may be that categories can't be filtered on a custom field. I think we were trying to optimise by not building a search index for those. If it's coming up as a requirement we'll revisit that.
It's not a big deal but I do believe that could be useful in some cases – though it doesn't seem that many people asked for this. In the end, though it isn't ideal, I still can use css to hide the category.
Thanks for your answer!
Quick follow-up on this, maybe it will be useful for some: I found a better way to show/hide specific categories on a given page. Thanks to Perch's flexibility, that works great.
Instead of using a dropdown in the category template (that would essentially set the visibility of the category item to
show
orhide
using a class), I now use a region with the category tagdisplay-as="checkboxes"
.What I like about it is that I can simply include the template of the category in my region template and that I don't have to use css to
display: none/block
the category item. For the content manager, it makes also more sense to decide which client to show on the home page from a region rather than in the category section of the CMS.Anyway, I'm again super pleased by how flexible Perch is. Great job!
Cheers.
I so appreciate the follow-up. Thank you!