In my last post, I showed a method for conditionally displaying facet blocks with Search Lucene, depending on the type of query being performed. This can also be adapted to work for Apache Solr, albeit with a different mechanism. The easiest way to do this with Apache Solr is to use the Context module to control which facet blocks are displayed, along with when and where they are displayed. In this example, I set up a context based on path, where that path was search/apachesolr/*. I then added all of my Apache Solr facet blocks in the context. With the context configured, I now have the ability to alter that context on the fly, based on whatever conditions I choose.
<?php
/**
* Implementation of hook_context_active_contexts_alter().
*
* <a href="http://twitter.com/param">@param</a> mixed $contexts
* Associative array of context objects
*/
function MYMODULE_context_active_contexts_alter(&$contexts) {
// If one of the active contexts is the apachesolr_search context, remove
// some unneccessary blocks
if(in_array('search-apachesolr-search', array_keys($contexts))) {
/**
* 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;
$query = apachesolr_current_query();
if($query) {
if($query->has_filter('type', 'biblio')) {
$show_biblio_facets = true;
}
}
// Loop through the apachesolr-search blocks and remove the Biblio facet blocks if needed
foreach($contexts['search-apachesolr-search']->block as $bid => $block) {
if(preg_match('/^apachesolr_biblio_/', $bid)) {
if(!$show_biblio_facets) {
unset($contexts['search-apachesolr-search']->block[$bid]);
}
}
}
}
}
?>
In this example, I get all of the active contexts, and if my apachesolr-search context is in effect, I look at the current Apache Solr query to see what filters have been applied. In the example, a flag is set to show the biblio facets if the current search is filtering by the biblio content type. There may be other methods for displaying facets conditionally in Apache Solr; however, I find this method to be fairly straightforward, and if you're not currently using Context for controlling block placement in Drupal, you should definitely reconsider that decision.
Recent comments
3 weeks 5 days ago
8 weeks 1 day ago
8 weeks 1 day ago
2 years 6 days ago
2 years 1 week ago
2 years 22 weeks ago
2 years 22 weeks ago
2 years 29 weeks ago
2 years 39 weeks ago
2 years 11 weeks ago