Search Lucene API: Conditionally Display Facet Blocks

I am working on a Drupal project that implements Search Lucene API, along with the Biblio module.  The client did not like the fact that when a generic search was executed, all enabled facet blocks were being displayed without context.  For example, a search for the generic term "data" with no filters showed Biblio-specific facet blocks, which didn't make any sense to the client.  He only wanted to display Biblio facet blocks if the current query was being filtered by content type where the content type was biblio.

After protesting mildly to the client, I realized that this request wasn't very difficult to achieve by implementing hook_luceneapi_facet_postrender_alter() in a custom module.  This implementation of the hook first checks to see if a search was executed, and if so, we retrieve the Zend query object in order to get the terms.  If the query is filtering by type where type is biblio, a flag is set to true to indicate that the Biblio facet blocks should be displayed.  If this flag is not set, the code loops through all the facet blocks ($items) passed into the hook, and removes the Biblio facets.

<?php 
function MYMODULE_luceneapi_facet_postrender_alter(&$items$realm$module$type NULL) {
  
// The example is only valid for "node" content.
  
if ($type != 'node') {
    return;
  }
  
  if (
$realm == 'block') {
    
/**
     * Look at the type of query that has been performed, and set a flag regarding whether 
     * or not to show the biblio facets if the type:biblio filter is in effect
     */
    
$show_biblio_facets false;
    if(
$module luceneapi_search_executed()) {
      
$query Zend_Search_Lucene_Search_QueryParser::parse(search_get_keys());
      if(
$query) {
        
module_invoke_all('luceneapi_query_alter'$query$module'node');
        
$terms $query->getQueryTerms();
        if(!empty(
$terms)) {
          foreach(
$terms as $term) {
            if(
$term instanceof Zend_Search_Lucene_Index_Term) {
              if(
$term->field == 'type' && $term->text == 'biblio') {
                
$show_biblio_facets true;
                break;
              }
            }
          }
        }
      }
    }
    
    if(!
$show_biblio_facets) {
      
// Loop through the items and remove the Biblio facet blocks if needed
      
if(!empty($items)) {
        foreach(
$items as $name => $item) {
          if(
preg_match('/^biblio_/'$name)) {
            unset(
$items[$name]);
          }
        }
      }
    }
  }
}

?>

Trackback URL for this post:

http://beyrent.net/trackback/227

Comments

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

About Erich

Erich is a web developer and a native New Englander who is passionate about life, the universe, and everything.

He is currently a senior Drupal developer at Harvard University, working on the IQSS OpenScholar project.  Prior to joining the team at Harvard, he was the engineering manager at CommonPlaces e-Solutions, in Hampstead, NH, contributing as the lead engineer on the Greenopolis.com and Twolia.com.

Erich is active in the Drupal community, having contributed modules and patches to the community. He presented at DrupalCon in Szeged Hungary, and co-presented at DrupalCon 2009 in Washington, DC.

Erich lives in New Hampshire with his wife, two sons, and two weimaraners.  When not writing code, Erich enjoys landscaping and woodworking.

Faceted search

Categories

Content type

Project types

Artwork Type

Artwork Tags

Recent comments

ebeyrent's tweets

Activity Stream

September 2, 2010

September 1, 2010

  • Twitter ebeyrent tweeted "@cpliakas LOL @webkenny as @acquia product" 11:23am #

August 31, 2010

  • Twitter ebeyrent tweeted "Dear @msnbc, I want to see the hurricane report, not a goddamn advertisement!! #thisisseriousshit" 12:08pm #
  • Twitter ebeyrent tweeted "Need a good D6 starter theme, looking at Blueprint, Fusion, and Omega. Any other recommendations? Preferences? #drupal" 11:35am #

August 30, 2010

  • Twitter ebeyrent tweeted "Having fun upgrading a D5 site to D6. #drupal" 3:05pm #
  • Twitter ebeyrent tweeted "@DrupalSnark Believe it or not, this was actually from a DrupalCamp presentation at Yale this weekend..." 11:38am #
  • Twitter ebeyrent tweeted "Today is an exciting and bittersweet milestone. My oldest son enters first grade..." 6:31am #

August 28, 2010

  • Twitter ebeyrent tweeted "Following @jjeff's presentation was like walking into the opera wearing my clown shoes. #drupalcampct" 4:38pm #
  • Twitter ebeyrent tweeted "Overlay module in #drupal 7 is awful. Don't use it. #drupalwtf" 4:31pm #
  • Twitter ebeyrent tweeted "Profile module in #drupal 7 doesn't use fields. Still. Don't use it. #drupalwtf" 4:29pm #
  • Twitter ebeyrent tweeted "Slides from the "Hack-proof Your Drupal App" presentation at #drupalcampct are online: http://bit.ly/9q9yRK" 4:19pm #
  • Twitter ebeyrent tweeted "@jjeff consults the orb at #drupalcampct" 2:42pm #
  • Twitter ebeyrent tweeted "Listening to @jjeff's presentation at #drupalcampct" 2:21pm #