10

I need to output some regions in the html.tpl.php template since these don't change between page.tpl.php and page-front.tpl.php (just to name two). Basically I want to avoid code duplication.

However there seems to be nothing associated with regions in html.tpl.php, nor I could retrieve anything using drupal_get_region_content (both with and without parameters it just returns an empty array).

Ideas?

3
  • Did you use drupal_set_region_content() to set the region content?
    – avpaderno
    Commented Dec 16, 2011 at 14:29
  • 2
    There's no such function. Maybe you meant drupal_add_region_content() but I see no point in it: I set regions' contents through the blocks admin interface.
    – Morpheu5
    Commented Dec 16, 2011 at 14:31
  • 1
    Yes, I meant drupal_add_region_content().
    – avpaderno
    Commented Dec 16, 2011 at 14:41

2 Answers 2

18

I solved like this:

function mytheme_preprocess_html(&$variables) {
    $variables['region_name'] = block_get_blocks_by_region('region_name');
}

and then I could do

<?php print render($region_name) ?>
5
  • Why use echo? Of course it works.
    – SDMulroy
    Commented Jan 9, 2015 at 13:13
  • Sure, if I were implementing a module or public theme I'd be compelled to follow community standards. This was a custom theme that was being used exclusively by my client.
    – Morpheu5
    Commented Dec 28, 2015 at 11:27
  • I know that this is an extremely old post, but I think that it's in poor judgement to accept an answer where the author of the answer lazily answers the question. e.g. You provide a function to add, but assume that all readers will magically know where this code should live. That's the lazy part and I'm down-voting this as it's not providing anything to novice drupal users/devs.
    – Skittles
    Commented Jul 31, 2016 at 18:39
  • I appreciate your input, and the fact that novices may not be getting a full explanation. However, please consider that a) spoonfeeding makes for lazy novices, and b) to anyone who has even swiftly read the docs (as any non-lazy novice should have) the placement of that function should be relatively obvious. If it isn't, then there is a manual for a good reason. My question (and answer) deals with an issue at a level where a certain basic knowledge of Drupal theming can be assumed. Furthermore, this is a Q&A site, the Docs site is next door.
    – Morpheu5
    Commented Aug 1, 2016 at 9:15
  • 1
    @Skitties We cannot duplicate here the documentation just because new users could not know what preprocess functions like the one shown in the question are. That is not even part of the question. If users have a specific question abput preprocess functions, they will ask a new question.
    – avpaderno
    Commented Aug 1, 2016 at 12:21
2

drupal_get_region_content() returns the content that has been set with previous calls to drupal_add_region_content(), but it doesn't include the content of the blocks, which are rendered in the page through block_page_build().

If you search for "drupal_add_region_content" in Drupal source code, you will notice that the function is called by:

  • drupal_get_region_content()
  • install_display_output()
  • template_preprocess_maintenance_page()
  • update_task_list()

What you set for the blocks in the administrative page is in which regions the blocks will be rendered, but their content is not added with drupal_add_region_content(); in fact, none of the functions listed above (which are the only functions calling drupal_add_region_content(), except the code for testing a Drupal installation) is for rendering/adding the block content.

If you need to render the blocks in your own code, you should check the code used in block_page_build(). In particular, this is the code that is probably more useful for you:

  // Fetch a list of regions for the current theme.
  $all_regions = system_region_list($theme);

  $item = menu_get_item();
  if ($item['path'] != 'admin/structure/block/demo/' . $theme) {
    // Load all region content assigned via blocks.
    foreach (array_keys($all_regions) as $region) {
      // Assign blocks to region.
      if ($blocks = block_get_blocks_by_region($region)) {
        $page[$region] = $blocks;
      }
    }
    // …
  }

The first two lines verify the page being shown is not the demo page used to show where the theme regions are placed.

If you need to render the blocks in a different way, it is more probable you are interested in overriding theme_block() in your theme, or (even more probably) creating a custom block.tpl.php template file. Another way to alter the way the blocks are rendered is to use a preprocess function for the blocks (hook_preprocess_block()).

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.