2

I want to add an og:url tag to the <head> so that the correct image shows up when my page is shared on Facebook.

I've found the hook_html_head_alter() hook which looks like I can add something to the head. I don't see how I can access the node object though when inside this method. I want to check if this page is for a certain Content Type. If it is that type, then look at some of the data on that node to determine which image to put in the og:url tag.

2 Answers 2

4

Drupal alter hooks, the group of hooks including hook_html_head_alter(), are not thought to add values, but to alter the values provided by another module. If you need to add a tag to the <head> of a node page, you should:

  • In a module, implement hook_node_view(), check $view_mode is not equal to 'teaser', and possibly other values for which you cannot provide the tag you want to add. Alternatively you can implement hook_entity_view(), which is called for every defined entity; if you just want to add the tag to nodes, you should rather implement hook_node_view().
    You can also implement hook_preprocess_node(), which is invoked every time a node is shown, or hook_preprocess_page(), which is invoked for every page, including the page containing the node edit form. In hook_preprocess_page(), you need to verify $variables['node'] is not empty; if it empty, it means the page is not about a node.
    The alternative is implementing hook_init(), but that hook is not invoked for cached pages.

  • In a theme, implement hook_preprocess_node(), or hook_preprocess_page().

In all the cases, you need drupal_add_html_head() to add the tag, and menu_get_object(), when you don't have the node object, which is available in hook_node_view(), hook_preprocess_node(), and hook_preprocess_page(). You would need to call menu_get_object() in hook_init(), for example.

Notice that menu_get_object() called without arguments returns a node object, if the page is about a node, and its path is similar to node/[node ID], or starts with that string; if the page page is, for example, page/node/[node ID] (where page/node/[node ID] is not a path alias), then menu_get_object() doesn't return a node object; in the example path I used, you should use menu_get_object('node', 2).

2

To answer your question you could use menu_get_object() inside hook_html_head_alter() to get the node object, as in $node = menu_get_object();.

As for solving the actual problem it would probably be more suitable to use drupal_add_html_head() and template_preprocess_node():

function mytheme_preprocess_node(&$variables) {
  $node = $variables['node'];
  if ($node->type == 'sometype') {
    $element = array(
      '#tag' => 'meta',
      '#attributes' => array(
        'property' => 'og:url',
        'content' => 'http://example.com/example',
      ),
    );
    drupal_add_html_head($element, 'og_url');
  }
}

Like kiamlaluno pointed out in his answer it's also possible to use some other preprocess/hook functions and you may want to do additional checks (looking at the view mode and page url for example - the arg() function may be useful with the latter) to make sure the tag is only added to the pages you want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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