Skip to content

Instantly share code, notes, and snippets.

@lewisnyman
Created May 26, 2013 00:22
Show Gist options
  • Save lewisnyman/5651296 to your computer and use it in GitHub Desktop.
Save lewisnyman/5651296 to your computer and use it in GitHub Desktop.
template_preprocess_html
/**
* Prepares variables for HTML document templates.
*
* Default template: html.html.twig.
*
* @param array $variables
* An associative array containing:
* - page: A render element representing the page.
*
* @see system_elements()
*/
function template_preprocess_html(&$variables) {
$language_interface = language(LANGUAGE_TYPE_INTERFACE); // DC
// 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'; // BAD
// Add a class that tells us whether we're on the front page or not.
$variables['attributes']['class'][] = $variables['is_front'] ? 'front'// GOOD : 'not-front'; // BAD
// 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'; // BAD
// Add information about the number of sidebars.
if (!empty($variables['page']['sidebar_first']) && !empty($variables['page']['sidebar_second'])) {
$variables['attributes']['class'][] = 'two-sidebars'; // BAD
}
elseif (!empty($variables['page']['sidebar_first'])) {
$variables['attributes']['class'][] = 'one-sidebar'; // BAD
$variables['attributes']['class'][] = 'sidebar-first'; // BAD
}
elseif (!empty($variables['page']['sidebar_second'])) {
$variables['attributes']['class'][] = 'one-sidebar'; // BAD
$variables['attributes']['class'][] = 'sidebar-second'; // BAD
}
else {
$variables['attributes']['class'][] = 'no-sidebars'; // BAD
}
// 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); // BAD
}
}
}
// 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); // GOOD
}
// Initializes attributes which are specific to the html and body elements.
$variables['html_attributes'] = new Attribute; // DC
// HTML element attributes.
$variables['html_attributes']['lang'] = $language_interface->langcode; // DC
$variables['html_attributes']['dir'] = $language_interface->direction ? 'rtl' : 'ltr'; // DC
// Add favicon.
if (theme_get_setting('features.favicon')) {
$favicon = theme_get_setting('favicon.url');// GOOD
$type = theme_get_setting('favicon.mimetype');// GOOD
drupal_add_html_head_link(array('rel' => 'shortcut icon', 'href' => drupal_strip_dangerous_protocols($favicon), 'type' => $type));// GOOD
}
$site_config = config('system.site');// GOOD
// Construct page title.
if (drupal_get_title()) {
$head_title = array(
'title' => strip_tags(drupal_get_title()),// GOOD
'name' => check_plain($site_config->get('name')),// GOOD
);
}
else {
$head_title = array('name' => check_plain($site_config->get('name')));// GOOD
if ($site_config->get('slogan')) {
$head_title['slogan'] = strip_tags(filter_xss_admin($site_config->get('slogan')));// GOOD
}
}
$variables['head_title_array'] = $head_title;// GOOD
$variables['head_title'] = implode(' | ', $head_title);// GOOD
// Display the html.tpl.php's default mobile metatags for responsive design.
$elements = array(// GOOD
'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')) {// GOOD
$variables['theme_hook_suggestions'] = $suggestions;
}
}
@carlwiedemann
Copy link

Most things in here whether good or bad have little to do with the theme layer, and aside from a few classes can probably be refactored elsewhere. I personally don't consider anything in the head tag to be presentational, FWIW.

Lines 51-54 should be in node.module. Not sure if there's an issue yet.

Lines 122-125 are getting refactored, see http://drupal.org/node/1886448

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment