12

I'm adding Open Graph support to a Drupal site and I have a bunch of drupal_add_html_head() calls, like:

  $og_title = array(
    '#tag' => 'meta',
    '#attributes' => array(
      'property' => 'og:title', 
      'content' => $node->title,
    ),
  );
  drupal_add_html_head($og_title, 'zujava_og_title');

 $og_url = array(
    '#tag' => 'meta',
    '#attributes' => array(
      'property' => 'og:url', 
      'content' => url('node/' . $node->nid, array('absolute' => TRUE)),
    ),
  );
  drupal_add_html_head($og_url, 'zujava_og_url');

In total, I have 10 of these. They don't seem to output in the same order they are called (all in a single function).

Is there some kind of weighting I can use to set the order?

15

Use the #weight property. As drupal_get_html_head() uses drupal_render() to render the meta tags, #weight is used when rendering them.

I uses the following code to make a test on my local site; it is the same code you are using, except it doesn't have any reference to the node object.

  $og_title = array(
    '#tag' => 'meta',
    '#attributes' => array(
      'property' => 'og:title', 
      'content' => "This is the title",
    ),
  );
  drupal_add_html_head($og_title, 'zujava_og_title');

 $og_url = array(
    '#tag' => 'meta',
    '#attributes' => array(
      'property' => 'og:url', 
      'content' => url('node/1', array('absolute' => TRUE)),
    ),
  );
  drupal_add_html_head($og_url, 'zujava_og_url');

  dsm(drupal_get_html_head());

The output I got is the following one.

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta property="og:url" content="http://tero.local/dr72/node/1" />
<meta name="Generator" content="Drupal 7 (http://drupal.org)" />
<meta property="og:title" content="This is the title" />

As you see, the last added tag is the first to appear.

I then run the following code.

  $og_title = array(
    '#tag' => 'meta',
    '#attributes' => array(
      'property' => 'og:title', 
      'content' => "This is the title",
    ),
    '#weight' => 10,
  );
  drupal_add_html_head($og_title, 'zujava_og_title');

 $og_url = array(
    '#tag' => 'meta',
    '#attributes' => array(
      'property' => 'og:url', 
      'content' => url('node/1', array('absolute' => TRUE)),
    ),
    '#weight' => 200,
  );
  drupal_add_html_head($og_url, 'zujava_og_url');

  dsm(drupal_get_html_head());

The output I got is the following one.

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="Generator" content="Drupal 7 (http://drupal.org)" />
<meta property="og:title" content="This is the title" />
<meta property="og:url" content="http://tero.local/dr72/node/1" />

As you see, the order of the meta tags has been changed; the meta tags added from the code appear after the default meta tags added from Drupal.

_drupal_default_html_head() (the function that returns the default meta tags) uses #weight for the "Content-Type" meta tag.

  $elements['system_meta_content_type'] = array(
    '#type' => 'html_tag', 
    '#tag' => 'meta', 
    '#attributes' => array(
      'http-equiv' => 'Content-Type', 
      'content' => 'text/html; charset=utf-8',
    ),
    // Security: This always has to be output first. 
    '#weight' => -1000,
  );
  • Awesome, thanks! THis worked. It seems like I missed something very obvious somewhere. For my own education, where did you find this documented? – Justin May 18 '12 at 13:46
  • 1
    Once I noticed the meta tags are rendered with drupal_render(), I have tried to see if #weight was used, as it is used for form elements which are rendered through the same function. The documentation for drupal_render() says: "Elements are sorted internally using uasort()." – kiamlaluno May 18 '12 at 14:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.