function template_preprocess_page

You are here

7 theme.inc template_preprocess_page(&$variables)
6 theme.inc template_preprocess_page(&$variables)
8 theme.inc template_preprocess_page(&$variables)

Prepares variables for the page template.

Default template: page.html.twig.

Most themes utilize their own copy of page.html.twig. The default is located inside "modules/system/page.html.twig". Look in there for the full list of variables.

Uses the arg() function to generate a series of page template suggestions based on the current path.

See also

drupal_render_page()

1 call to template_preprocess_page()
template_preprocess_maintenance_page in core/includes/theme.inc
Prepare variables for maintenance page templates.

File

core/includes/theme.inc, line 2101
The theme system, which controls the output of Drupal.

Code

function template_preprocess_page(&$variables) {
  $language_interface = \Drupal::languageManager()->getCurrentLanguage();
  $site_config = \Drupal::config('system.site');

  // Move some variables to the top level for themer convenience and template cleanliness.
  $variables['show_messages'] = $variables['page']['#show_messages'];
  $variables['title'] = $variables['page']['#title'];

  foreach (system_region_list($GLOBALS['theme']) as $region_key => $region_name) {
    if (!isset($variables['page'][$region_key])) {
      $variables['page'][$region_key] = array();
    }
  }

  // Set up layout variable.
  $variables['layout'] = 'none';
  if (!empty($variables['page']['sidebar_first'])) {
    $variables['layout'] = 'first';
  }
  if (!empty($variables['page']['sidebar_second'])) {
    $variables['layout'] = ($variables['layout'] == 'first') ? 'both' : 'second';
  }

  $variables['base_path'] = base_path();
  $variables['front_page'] = url();
  $variables['language'] = $language_interface;
  $variables['language']->dir = $language_interface->direction ? 'rtl' : 'ltr';
  $variables['logo'] = theme_get_setting('logo.url');
  $variables['site_name'] = (theme_get_setting('features.name') ? String::checkPlain($site_config->get('name')) : '');
  $variables['site_slogan'] = (theme_get_setting('features.slogan') ? Xss::filterAdmin($site_config->get('slogan')) : '');

  if (!defined('MAINTENANCE_MODE')) {
    $variables['main_menu'] = theme_get_setting('features.main_menu') ? menu_main_menu() : array();
    $variables['secondary_menu'] = theme_get_setting('features.secondary_menu') ? menu_secondary_menu() : array();
    $variables['action_links'] = menu_get_local_actions();
    $variables['tabs'] = menu_local_tabs();
    $variables['feed_icons'] = drupal_get_feeds();
  }
  else {
    $variables['main_menu'] = array();
    $variables['secondary_menu'] = array();
    $variables['action_links'] = array();
    $variables['tabs'] = array();
    $variables['feed_icons'] = '';
  }

  // Pass the main menu and secondary menu to the template as render arrays.
  if (!empty($variables['main_menu'])) {
    $variables['main_menu'] = array(
      '#theme' => 'links__system_main_menu',
      '#links' => $variables['main_menu'],
      '#heading' => array(
        'text' => t('Main menu'),
        'class' => array('visually-hidden'),
      ),
      '#set_active_class' => TRUE,
    );
  }
  if (!empty($variables['secondary_menu'])) {
    $variables['secondary_menu'] = array(
      '#theme' => 'links__system_secondary_menu',
      '#links' => $variables['secondary_menu'],
      '#heading' => array(
        'text' => t('Secondary menu'),
        'class' => array('visually-hidden'),
      ),
      '#set_active_class' => TRUE,
    );
  }

  if ($node = \Drupal::request()->attributes->get('node')) {
    $variables['node'] = $node;
  }

  // Prepare render array for messages. drupal_get_messages() is called later,
  // when this variable is rendered in a theme function or template file.
  $variables['messages'] = array(
    '#theme' => 'status_messages',
    '#access' => $variables['show_messages'],
  );

  // Set the breadcrumb last, so as to increase the chance of being able to
  // re-use the cache of an already retrieved menu containing the active link
  // for the current page.
  // @see menu_tree_page_data()
  if (!defined('MAINTENANCE_MODE')) {
    $variables['breadcrumb'] = array(
      '#theme' => 'breadcrumb',
      '#breadcrumb' => \Drupal::service('breadcrumb')->build(\Drupal::request()->attributes->all()),
    );
  }
}