7 theme.inc template_preprocess_html(&$variables)
8 theme.inc template_preprocess_html(&$variables)

Preprocess variables for html.tpl.php

See also

system_elements()

html.tpl.php

File

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

Code

function template_preprocess_html(&$variables) {
  // 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.).
  // Add a class that tells us whether we're on the front page or not.
  $variables ['classes_array'][] = $variables ['is_front'] ? 'front' : 'not-front';
  // Add a class that tells us whether the page is viewed by an authenticated user or not.
  $variables ['classes_array'][] = $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 ['classes_array'][] = 'two-sidebars';
  }
  elseif (!empty($variables ['page']['sidebar_first'])) {
    $variables ['classes_array'][] = 'one-sidebar sidebar-first';
  }
  elseif (!empty($variables ['page']['sidebar_second'])) {
    $variables ['classes_array'][] = 'one-sidebar sidebar-second';
  }
  else {
    $variables ['classes_array'][] = '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 ['classes_array'][] = drupal_html_class($suggestion);
      }
    }
  }

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

  // RDFa allows annotation of XHTML pages with RDF data, while GRDDL provides
  // mechanisms for extraction of this RDF content via XSLT transformation
  // using an associated GRDDL profile.
  $variables ['rdf_namespaces'] = drupal_get_rdf_namespaces();
  $variables ['grddl_profile'] = 'http://www.w3.org/1999/xhtml/vocab';
  $variables ['language'] = $GLOBALS ['language'];
  $variables ['language']->dir = $GLOBALS ['language']->direction ? 'rtl' : 'ltr';

  // Add favicon.
  if (theme_get_setting('toggle_favicon')) {
    $favicon = theme_get_setting('favicon');
    $type = theme_get_setting('favicon_mimetype');
    drupal_add_html_head_link(array('rel' => 'shortcut icon', 'href' => drupal_strip_dangerous_protocols($favicon), 'type' => $type));
  }

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

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

Comments

I have a number of views on my website that use a taxonomy term as a contextual filter. I am trying to get that taxonomy term into the page title.

i.e.

'Latest TAXONOMY_TERM News | Sitename etc.'

I am using taxonomy_term_load in a THEME_views_post_render function in my template.php to fetch the term and build the page title. I am then passing this variable to the THEME_preprocess_html where I use it to override $variables['head_title'].

i.e.

views_post_render:

$term = taxonomy_term_load($view->result[0]->_field_data['nid']['entity']->field_term['und'][0]['tid']);

$page_title = 'Latest ' . $term->name . ' News | ' . variable_get('site_name', 'Sitename etc');

preprocess_html:

$variables['head_title'] = $page_title ;

If I echo out $variables['head_title'] I can see the page title I have defined in THEME_views_post_render but it is clearly missing the taxonomy term.

i.e.

'Latest News | Sitename etc.'

Can anyone throw any light on what's happening here? I did think about doing all the logic within the THEME_preprocess_html function, but this doesn't seem to have access to the view.

Any help gratefully appreciated.

Rob.