I wanted to document this here just because it took me a little while to get all the bits working just right so I could have a hierarchical taxonomy display inside a Facet API search facet, rather than a flat display of only the taxonomy terms directly related to the nodes in the current search.
Basically, I had a search facet on a search page that allowed users to filter search results by a taxonomy term, and I wanted it to show the taxonomy's hierarchy:
To do this, you need to do two main things:
- Make sure your taxonomy field is being indexed with taxonomy hierarchy data intact.
- Set up the Facet API facet for this taxonomy term so it will display the full hierarchy.
Let's first start by making sure the taxonomy information is being indexed (refer to the image below):
- In Search API's configuration, edit the Filters for the search index you're using (e.g.
/admin/config/search/search_api/index/[index]/workflow
).- Make sure the 'Index hierarchy' checkbox is checked.
- In the 'Index hierarchy' Callback settings (which appear after you check the box in step 1), scroll down and make sure you select 'Parent terms' and 'All parent terms' under the Taxonomy type you need to display hierarchically.
- Save the Filters configuration, then reindex all the content on your site (otherwise Solr won't have the updated hierarchy information).
Next, we need to edit the Facet API facet for this taxonomy:
- Go to the taxonomy Facet's configuration page (e.g.
/admin/config/search/facetapi/search_api%40[index]/block/field_release/edit
). - Check the 'Expand hierarchy' checkbox under 'Display settings' (near the top of the form).
- Set 'Treat parent items as individual facet items' to 'No'.
- Set 'Flatten hierarchy' to 'No'.
- Set 'Minimum facet count' to 0 (to show all terms in the taxonomy).
After you've done that (make sure you reindexed your content!), you should have a nice hierarchical facet display.
Comments
Getting the hierarchy seems not to work through a entityreference field
(indexing a node type A with a reference to type B, which has the taxonomy on it).
Correction, it DOES work, also through an entity_reference. My CSS did hide the UL indents, so i did not see the difference at first.
I was hoping that hierarchies would also bring sorting by weight, but it does not. Seems like the Module facetapi_taxonomy_sort is the only way...
Glad you got it working, and yeah, the sorting is a little trickier.
Unless I'm mistaken (and there is a bug), selecting either of the options should work, "Parent terms" or "All parent terms" (which should really be called "All ancestors") – I'd suggest the former.
You could also add the taxonomy term reference as a related field and then index "Release » All parent terms" directly, without using "Index hierarchy". That's probably just a bit more complicated, though.
Hi Jeff,
Nice tutorial, I've tried this out but couldn't get it working. I'm using a database server, not a Solr server. Is Solr required to get this thing working?
Thanks!
Thank you very much for this tutorial, it works like a charm!
Thanks for writing this out! Do you happen to know how to do it for Drupal 8? The important step of selecting "parent terms" doesn't seem to be an option in 8. Thank you!
For Drupal 9 and 10, We can also use Facets Taxonomy Multilevel module, which extends feature from facet for displaying multiple facets based on their depth.