function hook_html_head_alter

You are here

7 system.api.php hook_html_head_alter(&$head_elements)
8 system.api.php hook_html_head_alter(&$head_elements)

Alter XHTML HEAD tags before they are rendered by drupal_get_html_head().

Elements available to be altered are only those added using drupal_add_html_head_link() or drupal_add_html_head(). CSS and JS files are handled using drupal_add_css() and drupal_add_js(), so the head links for those files will not appear in the $head_elements array.

Parameters

$head_elements: An array of renderable elements. Generally the values of the #attributes array will be the most likely target for changes.

Related topics

1 invocation of hook_html_head_alter()
drupal_get_html_head in includes/common.inc
Retrieves output to be displayed in the HEAD tag of the HTML page.

File

modules/system/system.api.php, line 3819
Hooks provided by Drupal core and the System module.

Code

function hook_html_head_alter(&$head_elements) {
  foreach ($head_elements as $key => $element) {
    if (isset($element['#attributes']['rel']) && $element['#attributes']['rel'] == 'canonical') {
      // I want a custom canonical URL.
      $head_elements[$key]['#attributes']['href'] = mymodule_canonical_url();
    }
  }
}

Comments

Unlike other hooks, this is a theme hook, so implementation is like:

<?php
THEME_NAME_html_head_alter
(&$head_elements) {
  [...]
}
?>

At least, that was the only way to get this one to actually do anything.

It can perfectly reside in a module, but it's called from the theming layer and will alter the theme it's called from.

Can anyone provide an example of adding a meta tag with this?

<meta http-equiv="X-UA-Compatible" content="IE=Edge" >

in your theme/module add (e.g. in your template.php)

/*
* Implements hook_html_head_alter
*/
function yourtheme_html_head_alter(&$head_elements) {

  // Force the latest IE rendering engine and Google Chrome Frame.
  $head_elements['chrome_frame'] = array(
    '#type' => 'html_tag',
    '#tag' => 'meta',
    '#attributes' => array('http-equiv' => 'X-UA-Compatible', 'content' => 'IE=edge,chrome=1'),
  );
}

or you could use the following in template.php

/*
* Implements hook_preprocess_html()
* Set up variables from the environment
*
*
*/
function yourtheme_preprocess_html(&$variables) {
// First, we must set up an array
$element = array(
    '#type' => 'html_tag',
    '#tag' => 'meta',
    '#attributes' => array('http-equiv' => 'X-UA-Compatible', 'content' => 'IE=edge,chrome=1'),
  );
drupal_add_html_head($element, 'chrome_frame');
}

iAugur: great recipe that worked for me as well. The one thing I had to add, though, was a #weight attribute to the render element. It's mentioned on the web that X-UA-Compatible must be the first tag that's in the output. This will do the trick:

"""
$element = array(
'#type' => 'html_tag',
'#tag' => 'meta',
'#attributes' => array('http-equiv' => 'X-UA-Compatible', 'content' => 'IE=edge,chrome=1'),
'#weight' => -1001,
);
"""

When implementing theme hooks in modules, the function name does not need to begin with the name of a theme. You can use the name of the theme engine (in most cases phptemplate) or as is done most commonly, just use template_ as the prefix.

/*
* Implements hook_html_head_alter
*/
function template_html_head_alter(&$head_elements) {
  // Force the latest IE rendering engine and Google Chrome Frame.
  $head_elements['chrome_frame'] = array(
    '#type' => 'html_tag',
    '#tag' => 'meta',
    '#attributes' => array('http-equiv' => 'X-UA-Compatible', 'content' => 'IE=edge,chrome=1'),
    '#weight' => -1001,
  );
}

I tried different methods to put similar codes into different files. I found that the best result is to add the following line in .htaccess :

Header Set X-UA-compatible: IE=Edge

However, my sharedhost provider does not like me to do so and may remove the line from the file.

So, I am now using 2 methods - the above line in .htaccess and your codes in template.php (as backup) - at the same time.

Hope this helpful.

Hi All, I created the function, but don't know where to call it.
I have also put the line in .htaccess. Not sure exactly where to put the line, so I put it at the beginning. Is that file used with windows/IIS?
Everything was fine with ie9, now with ie10 everything is wacko!

I've also tried fixing it via conditional_styles. Running out of ideas.

I'm developing a healthy hate for ie.
Thanks in advance.

Checkout Helicon Ape. It adds support for both .htaccess and .htpasswd configuration files in Windows IIS.