Search code examples
drupal-7

How to wrap H2 tags on field in a node in Drupal 7


I want to wrap h2 tags around a 'content type' text field (the field "product_title"), as a default setting, that the user cannot change.

I have tried to add this snippet in the file template.php in my theme:

function mytheme_preprocess_field(&$vars) {
if($vars['element']['#field_name'] == 'field_produktnamn'){
    $vars['items']['0']['#markup'] = '<h2>This works</h2>';
}

}

But I can't figure out how to keep the original field content. I only want to add h2 tags, not replace the text content.

Or maybe I should go about this another way?


Solution

  • try using this instead:

    $vars['items']['0']['#prefix'] = '<h2>';
    $vars['items']['0']['#suffix'] = '</h2>';