1

The problem:

I would like to be able to manage custom <meta> elements, on a per-item basis in a custom theme. An example would be for adding open graph and schema tags, but I'm really looking for a generic solution.

I'm aware of custom modules, such as Meta Tags, but I'd rather avoid relying on other modules at this point.

What I've Tried:

After looking at them_field it looked as though I should create a custom theme function for my custom field (field_test being used for this example). The theme function could add a <meta> tag and not return any content to be rendered.

In template.php:
<?php
//I *did* actually use my theme name instead of THEMENAME
function THEMENAME_field__field_test($vars) {
    drupal_add_html_head(array(
        '#tag' => 'meta',
        '#attributes' => array(
            'content' => drupal_render($vars['items'][0]),
            'name' => 'test'
        )
    ), 'test');

    //I figured this would prevent the field from being rendered inline
    return '';
}

Using that code the custom <meta> element was correctly added to the <head>. Nevertheless, the field was still rendered on the page.

If I changed the output to:

return ' ';

The content was no longer rendered to the page, but an unnecessary space character would be output where the field was to be rendered.

Is there a better way to render a field as a <meta> element?
-or-
How can I prevent this themed field from rendering inline?

1 Answer 1

-1

try to return drupal_add_html_head array directly

<?php

function THEMENAME_field__field_test($vars) {
    return drupal_add_html_head(array(
        '#tag' => 'meta',
        '#attributes' => array(
            'content' => drupal_render($vars['items'][0]),
            'name' => 'test'
        )
    ), 'test');
 }
3
  • This produced Array in the output.
    – zzzzBov
    Commented Nov 7, 2012 at 15:52
  • then use return drupal_render( your array) Commented Nov 8, 2012 at 7:17
  • I'm not sure I follow, could you update your answer with details?
    – zzzzBov
    Commented Nov 8, 2012 at 14:09

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.