I'm trying to add a simple class to a link outputted via print render($content['field_tags']). I'm using the Bootstrap theme. These are the approaches I've tried so far:

  1. Hooking into template_preprocess_node - no luck here as the html isn't generated yet.
  2. Hooking into template_field - this contains exactly what I expect but the links being generated don't obey the classes set here.
  3. Hacking node.tpl.php and manually outputting the terms - this sort of works but it's not the best solution.

This is the output as it is at the moment:

<a href="/tags/drupal" typeof="skos:Concept" property="rdfs:label skos:prefLabel" datatype="">drupal</a>

I don't want to add any more modules as there has to be a code solution to this, I'm not sure what I'm missing here. How do I add a class to link outputted in field_tags?


One option would be to preprocess the field:

function MYTHEME_preprocess_field(&$vars) {
  if ($vars['element']['#field_name'] == 'field_tags') {
    array_walk($vars['items'], function(&$el) { $el['#options']['attributes']['class'][] = 'some-class'; } );
  • Thanks that's a really nice way of doing it but something is still stripping all attributes. I've ruled out RDF module so will keep digging. I'll accept this answer as it will work. Edit: This is a specific problem for the Bootstrap theme.
    – digital
    May 5 '13 at 19:20
  • do you have any clue why this would not work? i see in my output that the attribute is added, but the term is not added any class at all.
    – Alex
    Nov 9 '13 at 23:26

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.