0

I am working with some view templates in Drupal 8, and I have the block ID of another view block I want to pull into the template. Is it possible to use the block ID to get the content of a block in a template?

If this is not possible in Twig, how could I achieve this otherwise?

2 Answers 2

0

Yes take a look at this module :

Twig Blocks

https://www.drupal.org/project/twig_blocks

After module install just call the block like this in the template :

{{ render_block('machine_name_of_your_block') }}

Or in a hook :

function hook_preprocess_page(&$variables)
{

    $block = \Drupal\block\Entity\Block::load('id_block');

    $variables['myBlock'] = \Drupal::entityTypeManager()
        ->getViewBuilder('block')
        ->view($block);

}

Then in your twig template :

  {{ myBlock }}
4
  • The greater issue is here is that I am using a plugin that lets the user choose whatever views block they want so I won't know what the machine name of the block is. Aug 29, 2017 at 15:29
  • i have update my answer
    – Kevin
    Aug 29, 2017 at 15:33
  • I would need to pass the block id into $block = \Drupal\block\Entity\Block::load(1234); ? Aug 29, 2017 at 15:53
  • yes you need to pass the block id into Drupal\block\Entity\Block::load()
    – Kevin
    Aug 29, 2017 at 15:54
0

Yes by using twig tweaks. See the cheat sheet for blocks and more.

The greater issue is here is that I am using a plugin that lets the user choose whatever views block they want so I won't know what the machine name of the block is

Pass the ID in as a variable and use

{# Print block using default configuration. #}
{{ drupal_block( block_id ) }}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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