function hook_page_alter

You are here

7 system.api.php hook_page_alter(&$page)
8 system.api.php hook_page_alter(&$page)

Perform alterations before a page is rendered.

Use this hook when you want to remove or alter elements at the page level, or add elements at the page level that depend on an other module's elements (this hook runs after hook_page_build().

If you are making changes to entities such as forms, menus, or user profiles, use those objects' native alter hooks instead (hook_form_alter(), for example).

The $page array contains top level elements for each block region:

  $page['page_top']
  $page['header']
  $page['sidebar_first']
  $page['content']
  $page['sidebar_second']
  $page['page_bottom']

The 'content' element contains the main content of the current page, and its structure will vary depending on what module is responsible for building the page. Some legacy modules may not return structured content at all: their pre-rendered markup will be located in $page['content']['main']['#markup'].

Pages built by Drupal's core Node and Blog modules use a standard structure:

  // Node body.
  $page['content']['system_main']['nodes'][$nid]['body']
  // Array of links attached to the node (add comments, read more).
  $page['content']['system_main']['nodes'][$nid]['links']
  // The node object itself.
  $page['content']['system_main']['nodes'][$nid]['#node']
  // The results pager.
  $page['content']['system_main']['pager']

Blocks may be referenced by their module/delta pair within a region:

  // The login block in the first sidebar region.
  $page['sidebar_first']['user_login']['#block'];

Parameters

$page: Nested array of renderable elements that make up the page.

See also

hook_page_build()

drupal_render_page()

Related topics

5 functions implement hook_page_alter()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

book_page_alter in modules/book/book.module
Implements hook_page_alter().
hook_system_themes_page_alter in modules/system/system.api.php
Alters theme operation links.
overlay_page_alter in modules/overlay/overlay.module
Implements hook_page_alter().
shortcut_page_alter in modules/shortcut/shortcut.module
Implements hook_page_alter().
system_page_alter in modules/system/system.module
Implements hook_page_alter().
1 invocation of hook_page_alter()
drupal_render_page in includes/common.inc
Renders the page, including all theming.

File

modules/system/system.api.php, line 1627
Hooks provided by Drupal core and the System module.

Code

function hook_page_alter(&$page) {
  // Add help text to the user login block.
  $page['sidebar_first']['user_login']['help'] = array(
    '#weight' => -10,
    '#markup' => t('To post comments or add new content, you first have to log in.'),
  );
}

Comments

Go to configurations -> performance and refresh your cache after implementing this "hook_page_alter" if you want to see results.

Lullabot has a podcast from Moshe Weitzman that discusses how to use this hook_page_alter, listen to it at http://www.lullabot.com/podcasts/drupal-voices-160-moshe-weitzman-on-pag...

I could not get $nid to be recognized. So, if you're wondering where this magical $nid comes from, do the below, as an example:

$node = menu_get_object();
$nid = $node->nid;
$page['content']['system_main']['nodes'][$nid]['body']

I have a node type called "innerpage" I created through the admin interface and I added a field called Link List (field_link_list) which is a list of links to anchor tags on the page. The links needed to be on the sidebar second. I could have made a block somehow I guess, but moving it to the sidebar was pretty easy... The code might help somebody.

function custom_page_alter(&$page) {
foreach($page['content']['system_main']['nodes'] as $nid => $nodes) {

if($page['content']['system_main']['nodes'][$nid]['body']['#bundle'] == 'innerpage') {
$page['content']['system_main']['nodes'][$nid]['field_link_list']['#label_display'] ='hidden';
$page['sidebar_second']['system_main']['nodes'][$nid]['field_link_list'] = $page['content']['system_main']['nodes'][$nid]['field_link_list'];
unset($page['content']['system_main']['nodes'][$nid]['field_link_list']);
}
}

Now that I look at it I suppose I could have checked isset() on the field being in the array instead of checking for the node type from "#bundle", but for what I did it works. Maybe I'll change it in case I need to use the field on a different node type.

The simple trick for me was to foreach through the nodes in $page['content']['system_main']['nodes'] to get the nid. Seems obvious but I didn't see it anywhere in any examples. Maybe there is an easier method?

Hope it helps...

This code moves one block to the top of the second side bar region when viewing content of type blog.

function THEMENAME_page_alter(&$page) {
  if (isset($page['sidebar_second'])) {
    if ($node = menu_get_object()) {
      if ($node->type == 'blog') {
        if (isset($page['sidebar_second']['cck_blocks_field_resources'])) {
          $page['sidebar_second']['cck_blocks_field_resources']['#weight'] = -100;
          $page['sidebar_second']['#sorted'] = FALSE;
        }
      }
    }
  }
}

If you forget to set #sorted, chances are that the block will not float up the list

Is there anyway to change the weight of js added using hook_page_alter relative to js added using hook_js_alter?

http://drupal.org/node/1945222#comment-7187556