function hook_page_build

You are here

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

Add elements to a page before it is rendered.

Use this hook when you want to add elements at the page level. For your additions to be printed, they have to be placed below a top level array key of the $page array that has the name of a region of the active theme.

By default, valid region keys are 'page_top', 'header', 'sidebar_first', 'content', 'sidebar_second' and 'page_bottom'. To get a list of all regions of the active theme, use system_region_list($theme). Note that $theme is a global variable.

If you want to alter the elements added by other modules or if your module depends on the elements of other modules, use hook_page_alter() instead which runs after this hook.

Parameters

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

See also

hook_page_alter()

drupal_render_page()

Related topics

11 functions implement hook_page_build()

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

block_page_build in core/modules/block/block.module
Implements hook_page_build().
contextual_page_build in core/modules/contextual/contextual.module
Implements hook_page_build().
dblog_page_build in core/modules/dblog/dblog.module
Implements hook_page_build().
filter_page_build in core/modules/filter/filter.module
Implements hook_page_build().
language_test_page_build in core/modules/language/tests/language_test/language_test.module
Implements hook_page_build().

... See full list

1 invocation of hook_page_build()
drupal_prepare_page in core/includes/common.inc
Processes the page render array, enhancing it as necessary.

File

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

Code

function hook_page_build(&$page) {
  $path = drupal_get_path('module', 'foo');
  // Add JavaScript/CSS assets to all pages.
  // @see drupal_process_attached()
  $page['#attached']['js'][$path . '/foo.js'] = array('every_page' => TRUE);
  $page['#attached']['css'][$path . '/foo.base.css'] = array('every_page' => TRUE);
  $page['#attached']['css'][$path . '/foo.theme.css'] = array('every_page' => TRUE);

  // Add a special CSS file to a certain page only.
  if (drupal_is_front_page()) {
    $page['#attached']['css'][] = $path . '/foo.front.css';
  }

  // Append a standard disclaimer to the content region on a node detail page.
  if (\Drupal::request()->attributes->get('node')) {
    $page['content']['disclaimer'] = array(
      '#markup' => t('Acme, Inc. is not responsible for the contents of this sample code.'),
      '#weight' => 25,
    );
  }
}