3

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?

1 Answer 1

2

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'; } );
  }
}
2
  • 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
    Commented May 5, 2013 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
    Commented Nov 9, 2013 at 23:26

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.