Add region to node.tpl with blocks from Context and normal blocks

You have a region that you want to add to a node.tpl. Regions are page level, not node level, so you need to add it as a block to node.tpl. In my case, blocks can be added to the region either in the blocks interface or using context (the site is moving to context, but some blocks are still done old school).

The trick to adding them to the node.tpl is to assign the blocks for the region to a variable and add the variable to the node.tpl (just like any other node.tpl variable). How you get the blocks for a given region will depend on whether the blocks are adding using Context or the blocks interface. The example below gets both and then merges them together.

In the example below, the region is called content_bottom.

The preprocess function in template.php:
function themename_preprocess_node(&$vars) {
  $context_content_bottom = array();
  $blocks_content_bottom = array();
  // Blocks that are assigned to the region using Context
  if ($plugin = context_get_plugin('reaction', 'block')) {
    $context_content_bottom = $plugin->block_get_blocks_by_region('content_bottom');
  // Blocks that are assigned to the region using the blocks interface
  if ($blocks = block_get_blocks_by_region('content_bottom')) {
    $blocks_content_bottom = $blocks;
  // Merge them together
  $vars['region']['content_bottom'] = array_merge($context_content_bottom, $blocks_content_bottom);

In the node.tpl:
    <?php if(isset($region) && $page): ?>
       <div id="content_bottom">
           <?php print render($region['content_bottom']); ?>
    <?php endif; ?>

If you liked this, you'll love my book, Master Drupal Module Development.

"..the must have drupal developers book"

Feeling stuck with Drupal 8 module dev?

Get the free 7 lesson course that will help you get started today without feeling overwhelmed.

  • Create Drupal modules with just a few commands using the Drupal Console
  • Create custom pages
  • Create custom blocks
  • Create admin forms
  • Demystify routers and controllers
  • Bonus material

Find out more

Add new comment

Plain text

  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.