1

I read the other similar questions, but it didn't work.

I created a block in a region and I want to display that same block on my custom page (inside the twig).

I'm using this code to load the block:

$block = \Drupal::entityTypeManager()->getStorage('block')->load('block_id');
$block_content =  \Drupal::entityTypeManager()->getViewBuilder('block')->view($block);

and I'm trying to build it like this:

$build['page'] = array(
      '#type' => 'inline_template',
      '#template' => $template,
      '#form' => $block_content,
);
return $build;

My twig file is like this:

<section>
  <tbody>
    <div>{{ form }}</div>
  </tbody>
</section>

But nothing is displayed... What am I doing wrong?

Thanks in advance.

0

To make the form variable available in the Twig template you need to supply a context like follows.

$build['page'] = [
  '#type' => 'inline_template',
  '#template' => $template,
  '#context' => ['form' => $block_content],
];

Generally, I would recommend using custom #theme hook instead of an inline template.

Another way to render block within a template is using Twig Tweak module.

{{ drupal_entity('block', 'block_id') }}

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.