Drupal 8 Tip: how to embed a region in a node template

Megan's picture

She has: 11,421 posts

Joined: Jun 1999

In Drupal 7, you could use get_blocks_by_region to get an array of blocks from a particular region, then pass that as a variable to your template file. In Drupal 8, blocks_get_blocks_by_region has been removed. View_get_view and hook_block_view have also been removed, so you can't use those to embed a block or view directly in a template file.

To embed a region in a node template, in Drupal 8, we can use entity_load_multiple_by_properties to get an array of the blocks assigned to that region, then use that to pass an array of blocks as a variable to the template file.

function yourtheme_preprocess_node(&$variables){
  $blocks = entity_load_multiple_by_properties('block', array('theme' => 'yourtheme', 'region' => 'yourregion'));

  uasort($blocks, 'Drupal\block\Entity\Block::sort');
  $build = array();
  foreach ($blocks as $key => $block) {
    if ($block->access('view')) {
      $build[$key] = entity_view($block, 'block');

  $variables['sidebar_region'] = $build;

Then you can render the sidebar_region variable in your twig template like this:

  {{ sidebar_region }}