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]);
}
}
}
}
}
}
?>
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