7.x common.inc drupal_add_region_content($region = NULL, $data = NULL)

Adds content to a specified region.


$region: Page region the content is added to.

$data: Content to be added.

5 calls to drupal_add_region_content()
DrupalSetContentTestCase::testRegions in modules/simpletest/tests/common.test
Test setting and retrieving content for theme regions.
drupal_get_region_content in includes/common.inc
Gets assigned content for a given region.
install_display_output in includes/install.core.inc
Displays themed installer output and ends the page request.
update_task_list in ./update.php
Adds the update task list to the current page.
_theme_test_drupal_add_region_content in modules/simpletest/tests/theme_test.module
Page callback, calls drupal_add_region_content.


includes/common.inc, line 175
Common functions that many Drupal modules will need to reference.


function drupal_add_region_content($region = NULL, $data = NULL) {
  static $content = array();

  if (isset($region) && isset($data)) {
    $content[$region][] = $data;
  return $content;


It seems there is a bug on this.


It seems drupal_add_region_content() is useful for core install module.

Don't recommend trying to use hook_page_build() plus drupal_add_region_content() to inject content where you want it. This seems to be intended for the install / update process.

I figured out a workaround for D7 on how to dynamically add content to a region.

On a site I had been building, I had the need to add random sidebar content on a per-page basis. These "callouts" are setup individually within the CMS and then referenced using Entity Ref to allow for reuse of the same callout on multiple pages. I used a helper function I built back for D6, that resides in my theme's template.php file to handle the processing of the referenced callouts, which I would call from a node.tpl.php file and pass the $node var. After this helper function was done processing, it would simply call "drupal_set_content" passing the built HTML, but when I updated the call to "drupal_add_region_content", I realized like everyone else...nothing rendered.

This got me thinking. We still have access to the $node var at the page.tpl.php level, and it is at this level where we render out all of our regions, so let's just make the call to add the sidebar content from the page.tpl.php file. I had already setup a region called "Sidebar" in my .info file, but I knew I would need a block to toss the generated HTML content into. I created a brand new block - I called it "Dynamic Sidebar Content", but title doesn't matter - and set the body to "nothing" (literally the word "nothing", as the Body field for a block is required). Upon examining the $page var, I found that I needed to overwrite the following: $page['sidebar']['block_3']['#markup'], where "sidebar" was the name of my region, "block_3" was the unique block id, and "#markup" was the Body content, which would be overwritten with my sidebar HTML. The last step in the process was to render the region where I wanted it within the HTML code in the page.tpl.php file.

Below is my code from my page.tpl.php file:

<div class="content-right sidebar"> <!-- start content-right section -->
	if (isset($node->field_sidebar_content[$node->language][0])) : 
		$sbContent = addSidebarContent($node);
		$page['sidebar']['block_3']['#markup'] = $sbContent;
		print render($page['sidebar']);
</div> <!-- end content-right section -->

I hope that this helps others who, like me, used to dynamically add stuff to regions, but lost this ability in D7. I hope that the D8 development teams will be bringing this ability back, as it was super handy.

If you are using Views, and have a "Page" generated by Views, the $node var is not defined at the page.tpl.php level, so we have to get more granular with our conditional:

<div class="content-right sidebar"> <!-- start content-right section -->
	if (isset($node) && isset($node->field_sidebar_content[$node->language][0])) : 
		$sbContent = addSidebarContent($node);
		$page['sidebar']['block_3']['#markup'] = $sbContent;
		print render($page['sidebar']);
</div> <!-- end content-right section -->

I found a solution to programmatically adding content to a region. I did this in hook_page_preprocess_page().

$variables['page']['rows_abovesidebar'][]['#markup'] = '<p>My html...</p>';

Replace 'rows_abovesidebar' with the region name. That does it!

Thanks chrisroane, works like a charm