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?

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 }}
  • 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. – beatsforthemind Aug 29 '17 at 15:29
  • i have update my answer – Kevin Aug 29 '17 at 15:33
  • I would need to pass the block id into $block = \Drupal\block\Entity\Block::load(1234); ? – beatsforthemind Aug 29 '17 at 15:53
  • yes you need to pass the block id into Drupal\block\Entity\Block::load() – Kevin Aug 29 '17 at 15:54

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.