Display facets on non-search pages

Last updated on
29 May 2019

Drupal 7 will no longer be supported after January 5, 2025. Learn more and find resources for Drupal 7 sites

One use case that sometimes arises is to display search facets on a page without any search. E.g., you want your front page to contain lists of the most common categories in several areas, along with their respective counts. When a visitor clicks on one of the categories, they should be redirected to your site's search, with the clicked category selected as a facet.

For this use case, there is the Views Facets block display provided by the search_api_facetapi sub-module. With it, you can easily display facet links for any search on any page you want. For this, you have two options:

Display facets in the Views block directly

Either create a new view on the search index you want to use, or edit an old one. Add a display of type "Facets block" to the view and configure the "Facet field" and "Search page path" in the "Block settings" section.
The facets will not show in the preview, so to test your view enable your new Views facets block in Administer > Structure > Blocks, like you would for all other blocks. Configure the block to show where and when you want it to appear.

Configure any (contextual) filters you want to apply to the result set used for the facet links (e.g., only count the categories for published nodes of a specific content type). Exposed filters are ignored. Format and fields settings are ignored, but you can also change the settings under "Other".

This will show a list of facet links to the selected search on the pages which were specified.
However, there are several limitations to this variant:

  • The facet links will always be styled as a list, no rewrite or format selection is possible.
  • Specifically, it is also not possible to use the normal Facet API widgets for these facets. This is because re-using the Facet API components from outside of the Facet API cannot be easily achieved.
  • If you want to display facets for more than one field, you have to add additional "Facets block" Views displays, and each will execute a separate search query when displayed.

Display facets in the Views block directly

To alleviate these shortcomings, there is a second variant:

Use Facet API to display facets

You can also just use the "Facets block" display to execute a search, and then use normal Facet API facet blocks for displaying facets on it. To do this, create a "Facets block" display like before, but set "Hide block" to "Yes" in the "Block settings". That way, on pages where the block would appear, a search query is executed, but the block is hidden from view.

Now, you can create facet blocks for this search like you normally would, on the index's "Facets" tab. You just have to make sure that the facet is active for the Facets block display (usually the ID will be search_api_views:[VIEWS_ID]-facets_block) in "Display for searches" / "Search IDs". Then, all the normal Facet API options can be used to define the Look & Feel of the displayed facet blocks. The facets in these blocks will automatically use the search path given in the Views Facets block's "Search page path" setting.

However, there are also some problems with this approach: first off, it is currently not possible to display the same facet in two different locations (#1411160: Allow a facet to be displayed multiple times in the same realm). Therefore, the facet list on the front page would have to be in the same region and place as for the search, and be styled the same way. Circumventing this in any way (either by cloning the facet block or by altering the display to move the block) currently still requires some custom coding (unless you are using Panels (or similar) to display your blocks).

The second problem is the one also described in the general Search API FAQs – for the facet blocks to work, you have to ensure that the view is executed before the blocks are rendered. However, since you can put the Views block into any region you like (since it won't be displayed in any case), it shouldn't be too much of a problem to find a position at which this requirement is met.

Add Facet Block into Panel

Add facet block into Panel

Help improve this page

Page status: No known problems

You can: