function template_preprocess_html

Same name in other branches
  1. 7.x includes/theme.inc \template_preprocess_html()
  2. 9 core/includes/theme.inc \template_preprocess_html()
  3. 10 core/includes/theme.inc \template_preprocess_html()
  4. 11.x core/includes/theme.inc \template_preprocess_html()

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 1285

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 = [
            // 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'),
        ];
    }
    elseif ($is_front_page) {
        $head_title = [
            '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;
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.