0

I've created a search with Search API module and Facets module. I've added two taxonomy term name fields to my search index and created two facets for those fields. Just for testing I've placed the facet blocks in my main content area, and fulltext search and faceted filtering are working as expected.

Now I want to place the facet blocks in the header of the view, and not in a page region. I've checked the block source file and ID of the facet blocks is facet_block. Facet blocks are derived blocks, so my best guess was that the block ID should be facet_block:MACHINE_NAME_OF_FACET.

I've added a Global: Rendered entity - Block to the header of the view. As block ID I've entered the schema from above, e.g. facet_block:museum for my "museum" facet.

However, the facet block doesn't show in the views header. (The same facets placed via block layout still work fine.)

Can anybody tell me how to render facet blocks inside a view header?

(I'm looking for a no-code-solution, without preprocessing or editing templates)

2
+50

Since you want a no-code solution, have you tried the Views Block Area module? It allows injection of blocks anywhere in a view. Once enabled you should see a "Block area" option in your view then select the facet you want shown in the view header. Should be able to do everything via the UI alone.

This module exposes all available blocks as a views area or field. This way you can add any block inside a field, footer or header in your view.

Blocks that are generated by views can not be selected, these blocks can not be inserted into views.

  • This is actually the solution I've been searching for. This module allows to include the actual blocks, while core's Rendered entity - block includes only block config entities (== machine names of blocks placed in Manage > Structure > Block layout). The core solution is not really useful when you want to render blocks inside a view, see the gotchas in Ismail's answer. – Hudri Mar 4 at 16:35
1

Firstly, I would recommend that you consider placing the facet block right above the view instead of performing a surgery on views :D

That said, here's what I think might work:

Solution 1: Views Header

  • Views allows you to attach rendered blocks in the views header.
  • In the views UI, click the Add button in the Header configuration.
  • Then choose Rendered Entity - Block and choose the facets block you want to display

Solution 2: Attachment Before

  • Write a hook_views_post_build()
  • In this hook, render the facet block as the attachment_before of the view.
  • You might face some difficulties with views caching, etc, but you can fine-tune the views build to alter the caching rules.
  • If you don't like the idea of Attachment Before, you can also consider adding a header item with a custom token like @facet:foo@ and then replace this custom token with a rendered facet block in hook_views_pre_render()

I hope this helps!

  • I'd love solution 1, but as I have already written, there is no "choose", only a text input that does not work as expected. – Hudri Feb 28 at 18:38
  • I used Solution 2 succesfully, although I had to implement hook_views_pre_render instead because hook_views_post_build caused an infinite loop. It was not needed to replace a token, simply something like $view->attachment_before[$facet_id] = $plugin_manager_block->createInstance("facet_block:$facet_id")->build(); is enough. – marcvangend Jun 14 at 8:51
0

This is an addition to @Jigar Mehta answer but I couldn't add it as comment because I needed more space.
There are some gotchas in Solution 1:
1. You need to create a facet and then place somewhere in the page.
2. You need to use the machine name of the block in Render Entity - Block form. No need to add facets:MACHINE_NAME_OF_FACET
3. The main issue is that the block needs to appear in the same page as the view so that it renders. It doesn't even render in the view preview section unless you save and visit that page.
4. I tried disabling the block while being placed in sidebar, or add a visibility rule that only makes it display on admin pages. Any rule that prevent its rendering prevents it from rendering in the view header.

Thus the only solution I see INMHO is to hide the actual block facet with CSS, but then this doesn't make sense since you can simply use the block on top of the view and not in the header unless there is a dire need for it.

Solution 3

You can also try this module Core Views Facet
I think it might solve your problem by having facets attached to a view as exposed filters.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.