Formatter which adds unique CSS class to every taxonomy term field item

Public

Two taxonomy term formatters which look and work similar to default Link and Plain Text ones, but add CSS class to an element. Useful when you want theme taxonomy terms depending on their tid. Paste this code into your custome module.

Get raw version
php
  1. /**
  2.  * Implements hook_field_formatter_info().
  3.  */
  4. function MODULE_NAME_field_formatter_info() {
  5. return array(
  6. 'taxonomy_term_reference_link_with_class' => array(
  7. 'label' => t('Link with class'),
  8. 'field types' => array('taxonomy_term_reference'),
  9. ),
  10. 'taxonomy_term_reference_plain_with_class' => array(
  11. 'label' => t('Plain text with class'),
  12. 'field types' => array('taxonomy_term_reference'),
  13. ),
  14. );
  15. }
  16.  
  17. /**
  18.  * Implements hook_field_formatter_view().
  19.  */
  20. function MODULE_NAME_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  21. $element = array();
  22.  
  23. // Terms whose tid is 'autocreate' do not exist
  24. // yet and $item['taxonomy_term'] is not set. Theme such terms as
  25. // just their name.
  26.  
  27. switch ($display['type']) {
  28. case 'taxonomy_term_reference_link_with_class':
  29. foreach ($items as $delta => $item) {
  30. if ($item['tid'] == 'autocreate') {
  31. $element[$delta] = array(
  32. '#value' => check_plain($item['name']),
  33. '#theme' => 'html_tag',
  34. '#tag' => 'span',
  35. '#attributes' => array(
  36. 'class' => array('term-tid-new'),
  37. ),
  38. );
  39. }
  40. else {
  41. $term = $item['taxonomy_term'];
  42. $uri = entity_uri('taxonomy_term', $term);
  43. $uri['options']['attributes']['class'][] = 'term-tid-' . $term->tid;
  44. $element[$delta] = array(
  45. '#type' => 'link',
  46. '#title' => $term->name,
  47. '#href' => $uri['path'],
  48. '#options' => $uri['options'],
  49. );
  50. }
  51. }
  52.  
  53. break;
  54.  
  55. case 'taxonomy_term_reference_plain_with_class':
  56. foreach ($items as $delta => $item) {
  57. $name = ($item['tid'] != 'autocreate' ? $item['taxonomy_term']->name : $item['name']);
  58. $tid = ($item['tid'] != 'autocreate' ? $item['taxonomy_term']->tid : 'new');
  59. $element[$delta] = array(
  60. '#value' => check_plain($name),
  61. '#theme' => 'html_tag',
  62. '#tag' => 'span',
  63. '#attributes' => array(
  64. 'class' => array('term-tid-' . $tid),
  65. ),
  66. );
  67. }
  68. break;
  69. }
  70.  
  71. return $element;
  72. }
  73.  
  74. /**
  75.  * Implements hook_field_formatter_prepare_view().
  76.  *
  77.  * This preloads all taxonomy terms for multiple loaded objects at once and
  78.  * unsets values for invalid terms that do not exist.
  79.  */
  80. function MODULE_NAME_field_formatter_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items, $displays) {
  81. // Call prepare view formatter for a taxonomy term
  82. taxonomy_field_formatter_prepare_view($entity_type, $entities, $field, $instances, $langcode, $items, $displays);
  83. }

Comments

kclarkson's picture

Hey Tim,

Is there a way to do this as theme preprocess function ?

Leo Jiménez's picture

Thanks man! You solved my world.

martin's picture

Does this work with terms in views? I will like to style taxonomy terms in views field differently. Can this do it

martin's picture

Bravo. It worked. Thanks a bunch

Marcoz's picture

Perfect!

(Don't forget to add the <?php tags when implementing in your code)