Secondary menu

Adding content to a region inside a node

aaron's picture

Adding content to a region inside a node

Written by Aaron Stewart on

We've all seen it, those darn blocks that have to appear in-line in a node body, but we're often left scratching our heads as to how to get it in there.

The problem is that regions are only surfaced to the page template, and the region we need to add is a div inside of a node.

Well, with a couple of simple theme overrides, you can get there, and make it performant as well. First, make sure you have the regions you want to place within your node defined in your .info file:

regions[left] = Left
regions[right] = Right
regions[header] = Header
regions[sub_header] = Sub-Header
regions[content_bottom] = Content Bottom
regions[upper_footer] = Upper Footer
regions[footer] = Footer
regions[node_inner_left] = Node Inner Left
regions[node_inner_right] = Node Inner Right

Note that in the above, we added two regions that we need to expose to the node template.

Next, override theme_preprocess_node as follows:

function yourtheme_preprocess_node(&$variables) {
  if ($variables['page'] != 0) {
    $variables['node_inner_left'] = theme('blocks', 'node_inner_left');
    $variables['node_inner_right'] = theme('blocks', 'node_inner_right');
  }
}

Lastly, override theme_blocks so that it doesn't try and render the regions more than once:

function yourtheme_blocks($region) {
  // store region content statically
  static $regions;
  if (!is_array($regions)) {
    $regions = array();
  }
  if ($regions[$region]) {
    return $regions[$region];
  }
  $output = '';
 
  if ($list = block_list($region)) {
    foreach ($list as $key => $block) {
      // $key == <i>module</i>_<i>delta</i>
      $output .= theme('block', $block);
    }
  }
 
  // Add any content assigned to this region through drupal_set_content() calls.
  $output .= drupal_get_content($region);
 
  $regions[$region] = $output;
  return $regions[$region];
}

That's it.. now $node_inner_left and $node_inner_right will be available within your node.tpl.php to do with as you please!