8

I am trying to place a block region inside a news content type but it is not rendering.

node--news.html.twig:

<article{{ attributes }} class="portal-article">

  <h1 class="article-title">{{node.label}}</h1>

  <p class="article-date">{{ date }}</p>

  <div class="article-main-content">
    {{ content }}
  </div>

  <div class="article-sidebar">
      {{ page.article_sidebar }}
  </div>

</article>

How can I get the block to render inside of a node?

4
  • 1
    Blocks are not within the scope of nodes. You basically want to construct a page with something like page_manager.
    – mradcliffe
    Commented Apr 4, 2016 at 15:19
  • This was definitely possible in Drupal 7. Possibly related, D8: drupal.stackexchange.com/questions/186246/… Commented Apr 4, 2016 at 17:39
  • 1
    No, it wasn't, not like this. As @mradcliffe said, if you want use blocks and regions like that, you need something like page_manager/panels/panelizer (the last is afaik not ported yet and the others are in progress). Display suite is also an option and allows you to use layouts in node templates.
    – Berdir
    Commented Apr 4, 2016 at 18:09
  • I should have clarified, in D7, you could do it with a preprocess function, create a variable and then render that in a node template. It looks like the same thing is possible with D8 with issue I linked to above. Commented Apr 5, 2016 at 16:56

1 Answer 1

9

This is probably the solution, "Making region content available to node templates in Drupal 8": http://atendesigngroup.com/blog/making-region-content-available-node-templates-drupal-8


Replace “THEME” with your theme name below:

/**
* Implements hook_preprocess_node() for NODE document templates.
*/
function THEME_preprocess_node(&$variables) {
  // Allowed view modes
  $view_mode = $variables['view_mode']; // Retrieve view mode
  $allowed_view_modes = ['full']; // Array of allowed view modes (for performance so as to not execute on unneeded nodes)

  // If view mode is in allowed view modes list, pass to THEME_add_regions_to_node()
  if(in_array($view_mode, $allowed_view_modes)) {
    // Allowed regions (for performance so as to not execute for unneeded region)
    $allowed_regions = ['primary_content'];
    THEME_add_regions_to_node($allowed_regions, $variables);
  }
}

/**
* THEME_add_regions_to_node
*/

function THEME_add_regions_to_node($allowed_regions, &$variables) {
  // Retrieve active theme
  $theme = \Drupal::theme()->getActiveTheme()->getName();

  // Retrieve theme regions
  $available_regions = system_region_list($theme, 'REGIONS_ALL');

  // Validate allowed regions with available regions
  $regions = array_intersect(array_keys($available_regions), $allowed_regions);

  // For each region
  foreach ($regions as $key => $region) {

    // Load region blocks
    $blocks = entity_load_multiple_by_properties('block', array('theme' => $theme, 'region' => $region));

    // Sort ‘em
    uasort($blocks, 'Drupal\block\Entity\Block::sort');

    // Capture viewable blocks and their settings to $build
    $build = array();
    foreach ($blocks as $key => $block) {
      if ($block->access('view')) {
        $build[$key] = entity_view($block, 'block');
      }
    }

    // Add build to region
    $variables[$region] = $build;
  }
}

After clearing caches, I can now print content specified in Drupal’s block layout using my node template files. For example, if I’ve specified primary_content in $allowed_regions, then I can access it via node--node_type.html.twig with:

{{ primary_content }}
4
  • This looks like a link only answer. Please include a relevant quote of it in your answer here ... Commented Jul 14, 2016 at 12:32
  • 2
    There is an error in the call to system_region_list(): The second argument is not a string, but an integer. Then, since the function is declared as system_region_list($theme, $show = REGIONS_ALL), there is no need to use the second argument, if you are going to use its default value.
    – apaderno
    Commented Jul 15, 2016 at 10:12
  • I done this add to my .theme and I can add drupal blocks into this custom region,but region template isn't applied or suggest.How do you do this ? I posted about that few time ago.thanks
    – user8753
    Commented Apr 12, 2017 at 15:06
  • It worked for me (also considering kiamlaluno comment). THANK YOU
    – Tritof
    Commented Mar 20, 2018 at 11:39

Your Answer

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

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