Community Documentation

drupal_add_html_head

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

theme_html_tag()

▾ 7 functions call 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().
rdf_preprocess_user_profile in modules/rdf/rdf.module
Implements MODULE_preprocess_HOOK().
_batch_progress_page_nojs in includes/batch.inc
Outputs a batch processing page without JavaScript support.

File

includes/common.inc, line 290
Common functions that many Drupal modules will need to reference.

Code

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

An 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&amp;subset=latin" rel="stylesheet" type="text/css" />

How about something more complex?

For example, an conditional statement <!--[if IE]> for some <script>s? I think I can form the script tags into an array based on your example, but I'm not sure how to handle the conditionals. For D6, this was just straight text with some variables thrown in. :)

script

i guess

http://api.drupal.org/api/drupal/includes--common.inc/function/drupal_ad...

is supposed to be used - maybe the settings could be used for your aim ..

#prefix / #suffix

Try adding the conditionals as a '#prefix' and '#suffix' to your element:

<?php
$element
= array(
 
'#tag' => 'script', // The #tag is the html tag - <link />
 
'#prefix' => '<!--[if IE]>',
 
'#attributes' => array( // Set up an array of attributes inside the tag
    // ...
 
),
 
'#suffix' => '<![endif]-->';
);
drupal_add_html_head($element, 'my_key');
?>

Works Perfectly more #value, #value_prefix, #value_suffix

The above method words perfectly, I wanted to embed the excanvas in drupal page and it needs to be embedded in the head. so I did this way.

<?php
$pathExcanvas
= drupal_get_path('module', 'embed_canvas') . '/excanvas.js';
   
$element = array(
       
'#tag' => 'script', // The #tag is the html tag - <link />
       
'#prefix' => '<!--[if IE]>',
       
'#suffix' => '<![endif]-->',
       
'#value_prefix' => '',
       
'#value'=>'',
       
'#value_suffix' => '',
       
'#attributes' => array(// Set up an array of attributes inside the tag
           
'type' => 'text/javascript',
           
'src' => $pathExcanvas,
        ),
    );
   
drupal_add_html_head($element, 'embed_canvas');
?>

The above code will output

<!--[if IE]><script type="text/javascript" src="sites/default/modules/embed_canvas/excanvas.js"></script>
<![endif]-->

You'll notice three more keys #value_prefix, #value_suffix,#value.
#value - alert('Hello World');
#value_prefix = put something like '<!--//--><![CDATA[//><!--'
#value_suffix - put something like '//--><!]]>'

More robust example can be found in drupal7 core file common.inc (line 4148, @see drupal_get_js())

Drupal core is always a nice place to look help regarding these kind of stuffs.

In you want to add some raw markup

you 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

Here 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

In 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

A 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');
?>

conditional comments

This all seems like a backward decision for me. I'm trying to add the conditional comments for IE, and I think there should be a way just to add the and tags into page.tpl.php.

Is there a way to get the setting to that as per what it was in drupal 6?

Maybe but might not work

You can put it in the page.tpl.php but it wont work for ie you can see this in the ie developer tools.

It really needs to be rendered at a higher level and called from the htacess file or in the template.php file.

Login or register to post comments