Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with drupal.stackexchange
scroll top