1

I'm working on a site where much of the back-end is handled by a site-specific custom module.

I have a sidebar region and within

Within module-thing.php is a call to <?php print render($page['sidebar']); ?> to print a region but it is not showing anything despite the region being set with content within the Blocks administration. I'm wondering if this is expected to work from within a module.. is there something i can do to show it?

I found this (right at the top) but doesn't seem to work in drupal 7 http://www.chrischen.me/content/drupal-how-access-regions-blocks-themes-or-modules..

2 Answers 2

1

The code to print page regions must be in your theme's page.tpl.php file, not in a file in a custom module.

If you want to put your code in a separate file (sidebar.php) you need to make sure it's included in page.tpl.php, like

include './sidebar.php'; // file is in the same folder as page.tpl.php

or

module_load_include('php', 'MYMODULE', 'sidebar'); // file is in a module folder

hope it helps

2
  • thanks! what can load a region? any tpl file in a theme? what about tpl files in modules?
    – Damon
    Commented May 22, 2012 at 15:25
  • I found this (right at the top) but doesn't seem to work in drupal 7 chrischen.me/content/…..
    – Damon
    Commented May 22, 2012 at 16:19
0

I found the easiest way was to do this:

<?php
  $region = block_get_blocks_by_region('region_name_here');
  print render($region);
?>

See the docs for more information: https://api.drupal.org/api/drupal/modules!block!block.module/function/block_get_blocks_by_region/7

In your module where you tried to print the sidebar region using the method used in a page template, use this snippet above instead. This method can also be used in html.tpl.php where the "usual" method cannot.

2
  • Hello. Code-only answers aren't usually that great. Person who asked, and future readers, are left on their own to figure out why, and that's bad. Especially if there is no obvious way.
    – Mołot
    Commented Dec 2, 2013 at 7:26
  • Thanks for the feedback. I've updated my answer to explain the solution a bit more. It's a handy Drupal function that I don't think is very well known.
    – Ryan
    Commented Dec 2, 2013 at 12:35

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.