I'm trying to rewrite the link text in an Apache Solr facet block, specifically the one to drill down by bundle/content type. By default the block displays the human readable name of the content type. I want to avoid simply editing the name as this would affect other things in the site.

So, in the block I would rather have a custom text such as 'Music Videos' rather than 'Article' from the 'article' content type.

I hoped to use the hooks created by Facet API Bonus to rewrite facet items via callback function, but am having difficulty getting the code to work.

Here is what I have, based on material from here and in the Facet API Bonus module readme:

function custom_module_facet_items_alter(&$build, &$settings)
  if ($settings->facet == "apachesolr@solr:block:bundle") {
    foreach ($build as $key => $item)
      switch ($key)
        case 'article':
          $build['article']['#markup'] = 'Music Videos';

I'm using Drupal 7.27, Facet API 7.x-1.5 and Facet API Bonus 7.x-1.1 .

Thanks in advance for any help.

  • 1
    Welcome to Drupal Answers :) What do you mean by "having difficulty getting the code to work", exactly? Do you get errors, have you confirmed the hook is being called by rendering some debug info to the screen, have you confirmed that $settings->facet is what you expect it to be, and so on? The more info you give the quicker and better we'll be able to help you
    – Clive
    Sep 4, 2014 at 15:23
  • Hi @Clive I haven't had any success. This is my first attempt at building a custom function. I've followed devel tutorials and think I have the correct machine name for the facet I want to target, but to be honest I'm completely unsure of how to implement the hook. The module documentation isn't great for a newbie, so if you have come across any other resources I could look over that would be great. Sep 4, 2014 at 15:31
  • 1
    The best resource you can use for debugging is the devel module. Install it, and whenever you want to inspect the contents of a variable use dpm($var); (it'll print to the messages area in your site). That way you can find out if your code is definitely being called and that the vars are what you expect
    – Clive
    Sep 4, 2014 at 15:32

1 Answer 1

  1. Make sure this filter is selected for your facet(s): "Rewrite facet items via callback function" admin/config/search/apachesolr/settings > Facets > YOUR FACET > configure filters > [x] Rewrite facet items via callback function
  2. You can verify your hook is implemented using drush and devel module:
drush hook facet_items_alter
Enter the number of the hook implementation you wish to view.
[0]  :  Cancel
[1]  :  my_module_name

If you see something similar to above, then your hook is implemented. To see the implementation code, enter a number (1 in this example).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.