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