Question

In Drupal, I render a list of taxonomy-items in the node template, using

print render($content['field_my_taxonomy']);

It displays a list like:

<ul class="links">
  <li class="taxonomy-term-reference-0">
       <a property="rdfs:label skos:prefLabel" typeof="skos:Concept" href="somewhere">
         Tomaten
       </a>
   </li>
   (...)

So far, so good. Now, I'd like to apply some of my own classes to both the li-tags and to the ul-tag.

I've managed to add a class to each li-tag via theme_preprocess_node:

$vars['content']['field_my_taxonomy'][$i]['#options']['attributes']['class'] = "my-li-class";

(where $i contains the index of the li-item).

However, I cannot find a similar procedure to add a class to the UL

I have found soultions involving creating template-files for specific fields, or involving overriding the theme function for taxonomy items alltogether. I don't like these solutions, because the involve adding quite a bit of "bloat" to my theme. I'd like to just add a new item to an array and let Drupal take care of the rendering. Surely, it must be possible to add ['attributes']['class']='my-ul-class to an array somewhere?

Was it helpful?

Solution

You could override the theme_item_list($variables) theme function or implement a hook_preprocess_HOOK function which would be hook_preprocess_item_list($variables) I think. The first would do your alterations in the theme itself, the second would do the alterations before going to the normal theme function.

links:

http://api.drupal.org/api/drupal/includes!theme.inc/function/theme_item_list/7

http://api.drupal.org/api/drupal/modules!system!theme.api.php/function/hook_preprocess_HOOK/7

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top