| 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 3807 - 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
PermalinkUnlike other hooks, this is a theme hook, so implementation is like:
<?phpTHEME_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.
Adding meta tag example
PermalinkCan anyone provide an example of adding a meta tag with this?
<meta http-equiv="X-UA-Compatible" content="IE=Edge" >Adding meta tag example
Permalinkin 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');
}
Need to add weight to element
PermalinkiAugur: 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,
);
"""
Theme hooks in modules can start with template_
PermalinkWhen 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,
);
}