hook_html_head_alter

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

File

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

Code

<?php
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

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.

Login or register to post comments