function 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.

See also

system_elements()

File

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

Code

function template_preprocess_html(&$variables) {
  $language_interface = language(Language::TYPE_INTERFACE);

  // Compile a list of classes that are going to be applied to the body element.
  // This allows advanced theming based on context (home page, node of certain type, etc.).
  $variables['attributes']['class'][] = 'html';

  // Add a class that tells us whether we're on the front page or not.
  $variables['attributes']['class'][] = $variables['is_front'] ? 'front' : 'not-front';

  // Add a class that tells us whether the page is viewed by an authenticated user or not.
  $variables['attributes']['class'][] = $variables['logged_in'] ? 'logged-in' : 'not-logged-in';

  // Add information about the number of sidebars.
  if (!empty($variables['page']['sidebar_first']) && !empty($variables['page']['sidebar_second'])) {
    $variables['attributes']['class'][] = 'two-sidebars';
  }
  elseif (!empty($variables['page']['sidebar_first'])) {
    $variables['attributes']['class'][] = 'one-sidebar';
    $variables['attributes']['class'][] = 'sidebar-first';
  }
  elseif (!empty($variables['page']['sidebar_second'])) {
    $variables['attributes']['class'][] = 'one-sidebar';
    $variables['attributes']['class'][] = 'sidebar-second';
  }
  else {
    $variables['attributes']['class'][] = 'no-sidebars';
  }

  // Populate the body classes.
  if ($suggestions = theme_get_suggestions(arg(), 'page', '-')) {
    foreach ($suggestions as $suggestion) {
      if ($suggestion != 'page-front') {

        // Add current suggestion to page classes to make it possible to theme
        // the page depending on the current page type (e.g. node, admin, user,
        // etc.) as well as more specific data like node-12 or node-edit.
        $variables['attributes']['class'][] = drupal_html_class($suggestion);
      }
    }
  }

  // If on an individual node page, add the node type to body classes.
  if ($node = menu_get_object()) {
    $variables['attributes']['class'][] = drupal_html_class('node-type-' . $node->type);
  }

  // Initializes attributes which are specific to the html and body elements.
  $variables['html_attributes'] = new Attribute();

  // HTML element attributes.
  $variables['html_attributes']['lang'] = $language_interface->langcode;
  $variables['html_attributes']['dir'] = $language_interface->direction ? 'rtl' : 'ltr';

  // Add favicon.
  if (theme_get_setting('features.favicon')) {
    $favicon = theme_get_setting('favicon.url');
    $type = theme_get_setting('favicon.mimetype');
    drupal_add_html_head_link(array(
      'rel' => 'shortcut icon',
      'href' => drupal_strip_dangerous_protocols($favicon),
      'type' => $type,
    ));
  }
  $site_config = config('system.site');

  // Construct page title.
  if (drupal_get_title()) {
    $head_title = array(
      'title' => strip_tags(drupal_get_title()),
      'name' => check_plain($site_config
        ->get('name')),
    );
  }
  else {
    $head_title = array(
      'name' => check_plain($site_config
        ->get('name')),
    );
    if ($site_config
      ->get('slogan')) {
      $head_title['slogan'] = strip_tags(filter_xss_admin($site_config
        ->get('slogan')));
    }
  }
  $variables['head_title_array'] = $head_title;
  $variables['head_title'] = implode(' | ', $head_title);

  // Display the html.tpl.php's default mobile metatags for responsive design.
  $elements = array(
    'MobileOptimized' => array(
      '#tag' => 'meta',
      '#attributes' => array(
        'name' => 'MobileOptimized',
        'content' => 'width',
      ),
    ),
    'HandheldFriendly' => array(
      '#tag' => 'meta',
      '#attributes' => array(
        'name' => 'HandheldFriendly',
        'content' => 'true',
      ),
    ),
    'viewport' => array(
      '#tag' => 'meta',
      '#attributes' => array(
        'name' => 'viewport',
        'content' => 'width=device-width',
      ),
    ),
    'cleartype' => array(
      '#tag' => 'meta',
      '#attributes' => array(
        'http-equiv' => 'cleartype',
        'content' => 'on',
      ),
    ),
  );
  foreach ($elements as $name => $element) {
    drupal_add_html_head($element, $name);
  }

  // Populate the page template suggestions.
  if ($suggestions = theme_get_suggestions(arg(), 'html')) {
    $variables['theme_hook_suggestions'] = $suggestions;
  }
}