W3cubDocs

/Drupal 8

function template_preprocess_html

template_preprocess_html(&$variables)

Prepares variables for HTML document templates.

Default template: html.html.twig.

Parameters

array $variables: An associative array containing:

  • page: A render element representing the page.

File

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

Code

function template_preprocess_html(&$variables) {
  $variables['page'] = $variables['html']['page'];
  unset($variables['html']['page']);
  $variables['page_top'] = NULL;
  if (isset($variables['html']['page_top'])) {
    $variables['page_top'] = $variables['html']['page_top'];
    unset($variables['html']['page_top']);
  }
  $variables['page_bottom'] = NULL;
  if (isset($variables['html']['page_bottom'])) {
    $variables['page_bottom'] = $variables['html']['page_bottom'];
    unset($variables['html']['page_bottom']);
  }

  $variables['html_attributes'] = new Attribute();

  // <html> element attributes.
  $language_interface = \Drupal::languageManager()->getCurrentLanguage();
  $variables['html_attributes']['lang'] = $language_interface->getId();
  $variables['html_attributes']['dir'] = $language_interface->getDirection();

  if (isset($variables['db_is_active']) && !$variables['db_is_active']) {
    $variables['db_offline'] = TRUE;
  }

  // Add a variable for the root path. This can be used to create a class and
  // theme the page depending on the current path (e.g. node, admin, user) as
  // well as more specific data like path-frontpage.
  $is_front_page = \Drupal::service('path.matcher')->isFrontPage();

  if ($is_front_page) {
    $variables['root_path'] = FALSE;
  }
  else {
    $system_path = \Drupal::service('path.current')->getPath();
    $variables['root_path'] = explode('/', $system_path) [1];
  }

  $site_config = \Drupal::config('system.site');
  // Construct page title.
  if (isset($variables['page']['#title']) && is_array($variables['page']['#title'])) {
    // Do an early render if the title is a render array.
    $variables['page']['#title'] = (string) \Drupal::service('renderer')->render($variables['page']['#title']);
  }
  if (!empty($variables['page']['#title'])) {
    $head_title = array(
      // Marking the title as safe since it has had the tags stripped.
      'title' => Markup::create(trim(strip_tags($variables['page']['#title']))),
      'name' => $site_config->get('name'),
    );
  }
  // @todo Remove once views is not bypassing the view subscriber anymore.
  //   @see https://www.drupal.org/node/2068471
  elseif ($is_front_page) {
    $head_title = array(
      'title' => t('Home'),
      'name' => $site_config->get('name'),
    );
  }
  else {
    $head_title = ['name' => $site_config->get('name')];
    if ($site_config->get('slogan')) {
      $head_title['slogan'] = strip_tags($site_config->get('slogan'));
    }
  }

  $variables['head_title'] = $head_title;
  // @deprecated in Drupal 8.0.0, will be removed before Drupal 9.0.0.
  $variables['head_title_array'] = $head_title;

  // Create placeholder strings for these keys.
  // @see \Drupal\Core\Render\HtmlResponseSubscriber
  $types = [
    'styles' => 'css',
    'scripts' => 'js',
    'scripts_bottom' => 'js-bottom',
    'head' => 'head',
  ];
  $variables['placeholder_token'] = Crypt::randomBytesBase64(55);
  foreach ($types as $type => $placeholder_name) {
    $placeholder = '<' . $placeholder_name . '-placeholder token="' . $variables['placeholder_token'] . '">';
    $variables['#attached']['html_response_attachment_placeholders'][$type] = $placeholder;
  }
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!includes!theme.inc/function/template_preprocess_html/8.1.x