By Evan


2016-04-04 14:57:08 8 Comments

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?

2 comments

@Alberto Silva 2016-07-14 12:30:13

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 }}

@Pierre.Vriens 2016-07-14 12:32:31

This looks like a link only answer. Please include a relevant quote of it in your answer here ...

@kiamlaluno 2016-07-15 10:12:31

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.

@webmaster pf 2017-04-12 15:06:49

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

@Tritof 2018-03-20 11:39:31

It worked for me (also considering kiamlaluno comment). THANK YOU

@JohnDoea 2016-07-14 13:59:35

As said in the comments, blocks are not in the scope of nodes. Nodes are usually situated in a region usually called "Main content region" or just "Main content".

Blocks could also be situated in the aforementioned "Main content" region but they are most likely to be found around that --- In what I love to call "Peripheral regions" which is actually any region besides "Main content".

If you want to change the layout of basic nodes in Drupal 8 (or 7) note that there is no necessity to customize template files for that, you could well use the GUI for that. I wouldn't advise to use Page manager but actually using "Paragraphs" module which gives you mobile-first responsive layouts for nodes, which are thus in accordance with the mobile-first responsive attitude of Drupal 8.

Related Questions

Sponsored Content

2 Answered Questions

[SOLVED] Render custom block using block.html.twig template

  • 2018-08-08 05:51:06
  • maskedjellybean
  • 759 View
  • 2 Score
  • 2 Answer
  • Tags:   8 blocks

2 Answered Questions

[SOLVED] Change Content region width when Sidebar has content?

1 Answered Questions

[SOLVED] Zebra stripping each block in a region

  • 2014-02-22 01:22:29
  • Jack Ryan
  • 98 View
  • 0 Score
  • 1 Answer
  • Tags:   blocks regions

1 Answered Questions

[SOLVED] How to reference a content-region block in node template?

  • 2012-07-24 16:01:10
  • David Csonka
  • 140 View
  • 0 Score
  • 1 Answer
  • Tags:   6 theming blocks

0 Answered Questions

change block region using template_preprocess_block() not working?

1 Answered Questions

Render a custom region inside node template

1 Answered Questions

[SOLVED] Place output of a custom module in a specific region

  • 2013-04-23 15:50:57
  • user145078
  • 582 View
  • 0 Score
  • 1 Answer
  • Tags:   7 blocks users nodes

1 Answered Questions

[SOLVED] API to display block in regions

  • 2013-01-14 08:36:46
  • arrubiu
  • 585 View
  • 0 Score
  • 1 Answer
  • Tags:   7 blocks regions

2 Answered Questions

[SOLVED] Create custom block with node title?

  • 2012-07-19 09:37:35
  • Anders Svensson
  • 1360 View
  • 1 Score
  • 2 Answer
  • Tags:   blocks regions

0 Answered Questions

Sponsored Content