Pointing All Taxonomy Term Links to ApacheSolr Search Results
I’ve been working on a project that leverages ApacheSolr for many of the displays on the site. One of the requirements is that every instance of a taxonomy term link on the site needs to point to search results pre-filtered (using FacetAPI) on the term. The other requirement is that if a taxonomy term’s vocabulary doesn’t have an associated search facet, the user should end up on the search page with results as if the user had searched for the term’s name in the search text box.
- If you see a “Climate Change” term (tid = 153) link from the “Topics” vocabulary (vid = 8), it should render as <a href="/search/site?f[0]=im_field_tr_topics%3A153">Climate Change</a> in the DOM.
- If you see a “Blog” term (tid = 123) link from the “Keywords” vocabulary (vid = 6), it should render as <a href="/search/site/blog">Blog</a> in the DOM.
To override all of the taxonomy term links we implement hook_entity_info_alter() to define a new uri callback for taxonomy_term entities. We can override the output in the callback function we define. Here’s what it looks like right now (I reduced the complexity for this demo). I’m sure I’ll have to tweak it some more, but it illustrates the basic strategy.
It’s important to note that the callback function needs to return an array of the variables that the url() function expects (notice how I used “options” param). See callback_entity_info_uri() for more info.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
/** * Implements hook_entity_info_alter(). */ function mymodule_entity_info_alter(&$entity_info) { $entity_info['taxonomy_term']['uri callback'] = 'mymodule_taxonomy_term_uri_callback'; } /** * URI callback for taxonomy terms * * @param object $term * Taxonomy term * * @return array * Array of variables to pass to the url() function * * @see mymodule_entity_info_alter() * @see callback_entity_info_uri() * @see entity_uri() */ function mymodule_taxonomy_term_uri_callback($term) { $term = i18n_taxonomy_localize_terms($term); $query_params = array(); switch ($term->vid) { case 8: // Topics $query_params['f'] = array('im_field_tr_topics:' . $term->tid); return array('path' => 'search/site', 'options' => array('query' => $query_params)); case 6: // Keywords don't have a search facet, so just search on the term name return array('path' => 'search/site/' . $term->name); } // Return the default return array('path' => 'taxonomy/term/' . $term->tid); |