Everyone likes search_api. And when you have a search page, there is usually no reason why you wouldn't add facets to it. Easy as pie, search_api and facetapi are two modules that just work.

At my last project however, I was in the process of theming the facet links and found that the link ID produced by facetapi is not consistent between page refreshing. And as the search page needs to refresh to show you your search results and take out irrelevant facets, that's hardly practical. The reason for this is that the ID is produced by a Drupal function called drupal_html_id(). This function makes sure that every ID is unique by counting elements and adding a numeral value to them. As the number of facets is narrowed down when we get closer to our search result, the added values will change. Probably there is a good reason for using this function, I'm certainly not the expert on this kind of programming, but as a themer, I like buttons to have a certain look, and I don't want them to switch costumes on a page refresh (something that actually happens).

I had a look in the facetapi issue cue and documentation where it is suggested that the easiest way is to make a specific widget for the purpose and use it from a custom module, so that is what I did. And since I am convinced laziness can sometimes be a virtue, I copied all the code from the default link widget, and replaced the drupal_html_id() with a function that turns the facet title into a css compatible string. This string goes right into the ID attribute of the facet array and that's it. Now every facet has its own, consistent ID and won't loose its look on page refresh. I have included the zip file of the module for anyone who is interested.

 // Add an ID to identify this link.
      $facetid = preg_replace('/\W+/','',strtolower(strip_tags($variables['text'])));
      $variables['options']['attributes']['id'] = 'facet-' . $facetid;


More information and alternatives on this problem http://drupal.org/node/1367612
I have also posted this module in the facetapi issue cue here : https://drupal.org/node/2005098

Reactie toevoegen

Plain text

  • Geen HTML toegestaan.
  • E-mail- en internetadressen worden automatisch aanklikbaar.
  • Regels en alinea's worden automatisch gesplitst.
By submitting this form, you accept the Mollom privacy policy.