Searching by custom attribute values (select/checkboxes)
Permalink
Hi all
I've created a custom attribute which stores a serialized string which contains page IDs, which are associated with the relevant page's name, which the user clicks to choose. The stored data looks like this:
I'd like to be able to search this attribute using the page names.
I've managed to display the existing values easily enough by creating search.php in /models/attribute/type/page_selector/, which looks like this:
The search() function in controller.php gets a Page List of the available pages for that instance of the attribute. This displays correctly, but I need to create some logic in searchForm() in controller.php (I think) to actually perform the search.
The searchForm() function in the Select attribute's controller.php seems like a good place to start, but I'm a bit perplexed by the last foreach and what $multiString is designed to do:
Any thoughts or ideas on this would be really helpful :)
I've created a custom attribute which stores a serialized string which contains page IDs, which are associated with the relevant page's name, which the user clicks to choose. The stored data looks like this:
a:1:{s:5:"value";a:2:{i:0;s:3:"166";i:1;s:3:"167";}}
I'd like to be able to search this attribute using the page names.
I've managed to display the existing values easily enough by creating search.php in /models/attribute/type/page_selector/, which looks like this:
foreach ($options as $key=> $option) { ?> <label class="checkbox inline"> <input type="checkbox" name="<?=$fieldPostName?>[]" value="<?=$key?>"/> <?=$option?> </label> <?php }
The search() function in controller.php gets a Page List of the available pages for that instance of the attribute. This displays correctly, but I need to create some logic in searchForm() in controller.php (I think) to actually perform the search.
The searchForm() function in the Select attribute's controller.php seems like a good place to start, but I'm a bit perplexed by the last foreach and what $multiString is designed to do:
public function searchForm($list) { $options = $this->request('atSelectOptionID'); $optionText = array(); $db = Loader::db(); $tbl = $this->attributeKey->getIndexedSearchTable(); if (!is_array($options)) { return $list; } foreach($options as $id) { if ($id > 0) { $opt = SelectAttributeTypeOption::getByID($id); if (is_object($opt)) { $optionText[] = $opt->getSelectAttributeOptionValue(true); $optionQuery[] = $opt->getSelectAttributeOptionValue(false); }
Viewing 15 lines of 32 lines. View entire code block.
Any thoughts or ideas on this would be really helpful :)
Anyway, first I've created my custom attribute and stored it as a serialised string using \n as the separator, in line with other similar functionality in c5 (I had previously used PHP's serialize() function but that caused complications at the searching stage):
/models/attribute/my_attribute/controller.php:
It's also worth creating a custom getDisplayValue() function to format how search results should look. In my case, the data that's stored is just page IDs, so outputting '123, 456' as the search result isn't exactly intuitive. getDisplayValue() takes care of this:
This outputs a snazzy list of page names which match the IDs of the attribute options the user has searched for.
Hope this helps someone.