I've created a facet block according to this example Programatically render a block in a twig template

I've created the facet block as follows:

$variables['block_output'] = Drupal::service('plugin.manager.block')->createInstance("facet_block:topic")->build();

That works great, but I'm missing the block template itself on my page (the block around the facet links output). I need a custom suggestion for this block to format it. Unfortunately, the hook_theme_suggestions_alter() doesn't detect this block. How can I add a file name suggestion?

Thank you!

  • try \Drupal::entityTypeManager()->getViewBilder('block')->view($block, $view_mode). Also you need the block entity, not plugin. Block entity basically bundles the plugin and configuration for it together. Discard previous, just entity_load('block', $id)->view($view_mode) should work. – user21641 Mar 23 '16 at 14:40
  • Yes, that works, assuming that you have a block. See drupal.stackexchange.com/questions/153184/…. build of the plugin just contains the content of the block. Page manager uses block plugins too and has its own templates. You could build a structure similar to what BlockViewBuilder does or you could simply add your markup around it yourself in your template. – Berdir Mar 23 '16 at 18:47
  • Thank you for your answers. Berdir, what do you mean with "build a structure similar to what BlockViewBuilder does" exactly? Can you make a example? Thank you very much! – marco-s Mar 30 '16 at 7:16

I'm using the module Twig Tweak, so you don't have to create a block, but you can add in in your twig file.

In my views-view--view-id--page.html.twig file I use it like this:

 {{ drupal_block('facet_block:facet_id') }}

Where the facet_id is the machine name of my facet.

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.