I need to put a meta NOINDEX tag in head conditionally to a CCK field value ("visibility=private") in one specific content type ("projects").

I read about drupal_add_html_head($data = NULL, $key = NULL) but I'm not sure where to put it.

If I could put the tag in body I would just use a computed_field, but as I need it to be in head, should I put it on my own module (but on which hook?) or should I copy/insert it in html.tpl.php ?

My problems are (a) that if html.tpl.php will change then I'll have to change also the copy I overridden; (b) then I'm also not sure I have access to $entity inside the template (I never used them) and, finally, (c) that being a matter of logic rather than presentation a module maybe is more indicated than a template. For all this reasons I would prefer a custom module. Would it be possible ?


It's definitely possible, you could do this using hook_entity_view() for example:

function MYMODULE_entity_view($entity, $type, $view_mode, $langcode) {
  if ($type == 'my_entity_type' && $view_mode == 'some_view_mode') {
    // Check the field items.
    if ($items = field_get_items('my_entity_type', $entity, 'field_visibility')) {
      if ($items[0]['value'] == 'private') {
        $element = array(
          '#tag' => 'meta',
          '#attributes' => array(
            'name' => 'robots',
            'content' => 'noindex',
        drupal_add_html_head($element, 'robots');
  • 1
    Thank you for the code, even more detailed answer than I expected. It works great. – Antonello Apr 8 '13 at 6:05

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.