Hi,

Please can you tell me how to rendering the block in a twig template?

I have try this with Twig Tweak but it not work.

{{ drupal_block('[fixed_block_id]') }}

I have not found docs about this.

Thank you!

Comments

Bessonweb created an issue. See original summary.

bessonweb’s picture

Issue summary: View changes
serverjohn’s picture

+1 on this. Would love to see how/if this is possible.

serverjohn’s picture

I figured out a way to get this to work with the UUID of the block that fixed block renders. To find the UUID export the single configuration of the fixed block (/admin/config/development/configuration/single/export). Don't use the fixed block UUID. As long as the fixed block has generated the block the following should work.

{{ drupal_block('block_content:009b4219-5fbe-49bf-9da5-c84f875fdb2a') }}

bessonweb’s picture

Thanks @serverjohn I will test this !

serverjohn’s picture

@Bessonweb You are welcome! However, after some testing I have found a hole in this solution. If the block is recreated, even though the config stays the same, it seems the UUID changes. So for in my real world test I imported a single fixed block into the configuration. Then I used the UUID in the configuration in my template. The block was not rendered. So all things considered I don't know that this it is possible to link to the fixed block. Only the block that is created by the fixed block can be referenced. I have just decided to manually change the twig templates when the blocks are recreated. e.g. ({{ drupal_entity('block_content', 6) }})

salvor_hardin’s picture

Hello, I found a way of printing a fixed block without using the UUID of the content block, that is a not a practical and consistent way of doing.
In a preprocess, use this :

      $block = \Drupal\fixed_block_content\Entity\FixedBlockContent::load('block_name'); // need to use the FixedBlockContent class
      $blockContent = $block->getBlockContent();
      $render = \Drupal::entityTypeManager()->getViewBuilder('block_content')->view($blockContent); // stay on block_content view builder
      $variables['block_name'] = $render;

Then just print {{ block_name }} in your template !

franknoel’s picture

@Salvor_Hardin Your solution is great, but it only renders the block content. If you also want to have the block template, you can provide it with the render array's `#theme` key. For exemple:

public static function getRenderedFixedBlock($block_id) {
  $output = [];
  if ($block = FixedBlockContent::load($block_id)) {
    $content = $block->getBlockContent();
    $output = [
      '#theme' => 'block__' . $block->bundle(),
      'content' => \Drupal::entityTypeManager()->getViewBuilder('block_content')->view($content),
    ];
  }
  return $output;
}
mark_gerarts’s picture

Since you're using the Twig Tweak module, you can do this as well:

{{ drupal_block('fixed_block_content:your_fixed_block_machine_name') }}