Sky Blue Sofa Blog Indexing of Single Selects in Concrete5 - Sky Blue Sofa Blog

Indexing of Single Selects in Concrete5

Posted by Dave Rogers // February 1, 2015 // in Blog // 0 Comments

Letters on a page

Doing a MySQL search on select values in concrete5 version 5.6.3.1 (perhaps earlier versions, too) is somewhat of a pain. In order to get each of the selected values of a mutliselect box, newline characters are added between values.

This causes big issues when you're using Page List+ select boxes and trying to do something like:

Show all 'House' pages that have 'x' number of bathrooms or more

The default functionality looks like this:

  1. public function getSearchIndexValue() {
  2. $str = "\n";
  3.  
  4. $list = $this->getSelectedOptions();
  5. foreach($list as $l) {
  6. $l = (is_object($l) && method_exists($l,'__toString')) ? $l->__toString() : $l;
  7. $str .= $l . "\n";
  8. }
  9. // remove line break for empty list
  10. if ($str == "\n") {
  11. return '';
  12. }
  13. return $str;
  14. }
  15.  

You'll see that before and after each value in the index, a newline character, \n, is added.

To resolve this, I've used the default core overwrite functionality:

  1. Copy the file located at {root}/concrete/models/attribute/types/select/controller.php to {root}/models/attribute/types/select/controller.php
  2. Add the updated functionality (shown below) to the SelectAttributeTypeController class.
  3. Clear the overwrites cache.
  1. <?php defined('C5_EXECUTE') or die("Access Denied.");
  2. class SelectAttributeTypeController extends Concrete5_Controller_AttributeType_Select {
  3. public function getSearchIndexValue() {
  4. $ignoreNewlines = in_array($this->getAttributeKey()->getAttributeKeyHandle(), array('my_single_select_attribute'));
  5. if (!$ignoreNewlines) {
  6. $str = "\n";
  7. }
  8. $list = $this->getSelectedOptions();
  9. foreach($list as $l) {
  10. $l = (is_object($l) && method_exists($l,'__toString')) ? $l->__toString() : $l;
  11. $str .= $l;
  12. if (!$ignoreNewlines) {
  13. $str .= "\n";
  14. }
  15. }
  16. // remove line break for empty list
  17. if ($str == "\n") {
  18. return '';
  19. }
  20. return $str;
  21. }
  22. }
  23. class SelectAttributeTypeOption extends Concrete5_Model_SelectAttributeTypeOption {}
  24. class SelectAttributeTypeOptionList extends Concrete5_Model_SelectAttributeTypeOptionList {}

You can download this file here. Note that it is in text format; you'll need to change the extension.

Once in place, you'll also need to update the list of select attribute handles that you want it to affect. Change line 4 to do this.

About the Author

Dave Rogers

Dave is the founder of Sky Blue Sofa Web Design. He enjoys working out, spending time with his wife and dogs and programming. He grew up and currently resides in the Illinois Quad Cities. You can find his personal blog at strength/reliance.com.

View Profile »

Comments

You must be logged in to leave a reply. Login »