| 7 common.inc | drupal_add_html_head($data = NULL, $key = NULL) |
| 8 common.inc | drupal_add_html_head($data = NULL, $key = NULL) |
Adds output to the HEAD tag of the HTML page.
This function can be called as long as the headers aren't sent. Pass no arguments (or NULL for both) to retrieve the currently stored elements.
Parameters
$data: A renderable array. If the '#type' key is not set then 'html_tag' will be added as the default '#type'.
$key: A unique string key to allow implementations of hook_html_head_alter() to identify the element in $data. Required if $data is not NULL.
Return value
An array of all stored HEAD elements.
See also
7 calls to drupal_add_html_head()
- drupal_add_html_head_link in includes/
common.inc - Adds a LINK tag with a distinct 'rel' attribute to the page's HEAD.
- drupal_get_html_head in includes/
common.inc - Retrieves output to be displayed in the HEAD tag of the HTML page.
- openid_test_yadis_http_equiv in modules/
openid/ tests/ openid_test.module - Menu callback; regular HTML page with <meta> element.
- rdf_preprocess_node in modules/
rdf/ rdf.module - Implements MODULE_preprocess_HOOK().
- rdf_preprocess_taxonomy_term in modules/
rdf/ rdf.module - Implements MODULE_preprocess_HOOK().
File
- includes/
common.inc, line 299 - Common functions that many Drupal modules will need to reference.
Code
function drupal_add_html_head($data = NULL, $key = NULL) {
$stored_head = &drupal_static(__FUNCTION__);
if (!isset($stored_head)) {
// Make sure the defaults, including Content-Type, come first.
$stored_head = _drupal_default_html_head();
}
if (isset($data) && isset($key)) {
if (!isset($data['#type'])) {
$data['#type'] = 'html_tag';
}
$stored_head[$key] = $data;
}
return $stored_head;
}
Comments
An example, for those who are
PermalinkAn example, for those who are a bit confused as to the change from Drupal 6 to 7. Here's how I added the
<link>attribute for Google's Font API inclusion to a Drupal 7 site:<?php// First, we must set up an array
$element = array(
'#tag' => 'link', // The #tag is the html tag - <link />
'#attributes' => array( // Set up an array of attributes inside the tag
'href' => 'http://fonts.googleapis.com/css?family=Cardo&subset=latin',
'rel' => 'stylesheet',
'type' => 'text/css',
),
);
drupal_add_html_head($element, 'google_font_cardo');
?>
This will output the following HTML:
<link href="http://fonts.googleapis.com/css?family=Cardo&subset=latin" rel="stylesheet" type="text/css" />In you want to add some raw markup
Permalinkyou can use the markup type. For example, adding some jquery tmpl templates:
<?php$inline_script = <<<EOL
<script id="tp1" type="text/x-jquery-tmpl"><li>\${Name}</li></script>
<script id="tp2" type="text/x-jquery-tmpl"><li>\${date}</li></script>
EOL;
$element = array(
'#type' => 'markup',
'#markup' => $inline_script,
);
drupal_add_html_head($element, 'jquery-tmpl');
?>
THEMERS: to accomplish this cleanly in the theme layer
PermalinkHere is an example for adding the IE meta tag to force the rendering engine with some chrome frame love thrown in:
Goal is to get this meta tag in the header of your theme:
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">Use the theme_preprocess_html hook, so add this snippet to your template.php file:
/**
* Preprocesses the wrapping HTML.
*
* @param array &$variables
* Template variables.
*/
function YOUR_THEME_NAME_preprocess_html(&$vars) {
// Setup IE meta tag to force IE rendering mode
$meta_ie_render_engine = array(
'#type' => 'html_tag',
'#tag' => 'meta',
'#attributes' => array(
'content' => 'IE=edge,chrome=1',
'http-equiv' => 'X-UA-Compatible',
)
);
// Add header meta tag for IE to head
drupal_add_html_head($meta_ie_render_engine, 'meta_ie_render_engine');
}
In some cases IE will ignore
PermalinkIn some cases IE will ignore this rendering mode directive unless it's the first tag to appear after the opening head tag. This can be achieved by specifying a very low weight property as follows:
$meta_ie_render_engine = array('#type' => 'html_tag',
'#tag' => 'meta',
'#attributes' => array(
'http-equiv' => 'X-UA-Compatible',
'content' => 'IE=edge,chrome=1',
),
'#weight' => '-99999',
);
A quick way for adding a
PermalinkA quick way for adding a specific opengraph image from your nodes is by adding this to the relevant node.tpl.php.
<?php$imgpath = image_style_url('YOUR_STYLE', $YOUR_FIELD[0]['uri']);
$element = array(
'#tag' => 'meta',
'#attributes' => array(
'property' => 'og:image',
'content' => $imgpath,
),
);
drupal_add_html_head($element, 'og_image');
?>
Can you do this with conditional HTML?
PermalinkCan you do this with conditional HTML? How would I go about getting this into my head?
<!--[if (gte IE 6)&(lte IE 8)]><script type="text/javascript" src="selectivizr.js"></script>
<noscript><link rel="stylesheet" href="[fallback css]" /></noscript>
<![endif]-->
@danbohea
Permalink@danbohea
This is how I did it using MYTHEME_preprocess_html in template.php:
$path = $GLOBALS['base_url'] . '/' . path_to_theme() . '/js/selectivizr-min.js';$selectivizr = array(
'#tag' => 'script',
'#attributes' => array(
'src' => $path,
),
'#prefix' => '<!--[if lte IE 8]>',
'#suffix' => '</script><![endif]-->',
);
drupal_add_html_head($selectivizr, 'selectivizr');
Can it be used inside modules (not template) ?
PermalinkHello, can drupal_add_html_head be used within a module (od even a computed field), or does it need to be used inside a template?
I need to put a tag if a CCK field is "private" and I need to do it in head.. I know how to handle the logic but I would prefer using this function within a module or a computed_filed, as I don't already have overriden templates.
Would it be possible ?
inside module
Permalinkof course you can and you have to.
I don't like this new format for drupal 7 because script tag don't add the closer tag and occur errors.
I will use the inline method, thank you @mtsanford
EDIT
PermalinkOh i didn't see the '#suffix' => '' option