Software engineer, data guy, Open Source enthusiast, New Hampshire resident, husband, father. Fan of guitars, hiking, photography, homebrewing, sarcasm.
Drupal 6: Creating a custom Lucene Search facet from taxonomy and integrating into the search form block
In this tutorial I’ll show how you to create a custom Lucene Search facet from taxonomy and integrate into the search form block. A great first step would be to review the Drupal Lucene API documentation (see luceneapi.api.php PHP file in the luceneapi module folder).
I assigned the search form block to a region in my theme (admin/build/block), which generates this form:
I added a new vocabulary called “Topics”, and added a few terms (admin/content/taxonomy).
The goal of this code is to integrate the Topic vocabulary into the search form block to allow the user to select a taxonomy term as they search. To start, you need to implement a hook_luceneapi_facet_realm() and a callback function in your custom module.
At this point if you go to the facets admin page (admin/settings/luceneapi_node/facets), you can see the newly created realm. I assigned the taxonomy vocabulary “Topic” to this realm.
Up next is implementing a hook_form_alter() to integrate the facet into the search form.
The search from block should now show an empty “Topic” facet.
The last piece of code implements hook_luceneapi_facet_postrender_alter() which gives you the opportunity to modify the facet, and in the this case, add its options.
Immediately after implementing this hook, if you krumo() or dsm() the $items argument, you’ll see the form element has no options.
The next section of code copied the contrib module code in “Lucene Node”. [See file: luceneapi/contrib/luceneapi_node/luceneapi_node.module; function: function luceneapi_node_luceneapi_facet_postrender_alter()]
Reloading the page will now show the search form with a completed facet.
Submitting the search form block with a selected taxonomy term will now take the user to the search results page with the taxonomy facet pre-selected!