hook_page_alter

  1. drupal
    1. 8
    2. 7
Versions
8 – 7 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:

<?php
  $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:

<?php
  // 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:

<?php
  // 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

Code

modules/system/system.api.php, line 1567

<?php
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.'),
  );
}
?>