1

My module has a menu callback which outputs data to a custom template using hook_theme().

Normally, Drupal includes this output into the content region that is rendered at the page.tpl.php level using render($page['content']).

How can I simultaneously output stuff from my module but into other regions of page.tpl.php? For example $page['sidebar_first'] or $page['footer'].

Is this possible?
Alternatively, how can I tell Drupal to use a custom page.tpl.php when the menu callback is called?

2

If you want to exactly put content in $page['sidebar_first'], then you can implement hook_page_build() or hook_page_alter(). The difference between the hooks is that the latter is used when the module needs to alter some elements added by another module with hook_page_build() as the first hook is executed first.

Examples of hook_page_build() are the following functions.

function toolbar_page_build(&$page) {
  $page['page_top']['toolbar'] = array(
    '#pre_render' => array('toolbar_pre_render'), 
    '#access' => user_access('access toolbar'), 
    'toolbar_drawer' => array(),
  );
}
function dashboard_page_build(&$page) {
  global $theme_key;

  if (dashboard_is_visible()) {
    $block_info = array();

    // Create a wrapper for the dashboard itself, then insert each dashboard
    // region into it.
    $page['content']['dashboard'] = array('#theme_wrappers' => array('dashboard'));
    foreach (dashboard_regions() as $region) {
      // Do not show dashboard blocks that are disabled.
      if ($region == 'dashboard_inactive') {
        continue;
      }
      // …
    }
    // …
  }
}

As alternative, your module could define a block, but in that case, the block will be placed in the region set by the administrator users (or any user with the right permission), which could be place the block in a different region. If there isn't a reason to place the content your module is adding in a specific region, then I would rather opt for implementing blocks.

  • great! thanks kiamlaluno, I think this will work for me. can this hook be executed ONLY by a menu callback function? or do I have to use if statements within the hook to determine if it's the right page? – Carlos Muñiz Jun 27 '11 at 0:41
  • @Charlie Hooks are not executed by a menu callback; Drupal will invoke the hook when necessary. The hook needs to verify some conditions, before to act, or it will alter any page. If the code needs to be always executed, then checking in which page is being invoked is not required. Using an access callback is alternative of checking some conditions. – kiamlaluno Jun 27 '11 at 7:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.