Drupal 7: Render Tags in a Comma Delimited List
Drupal 7 does not print taxonomy tags in a comma delimited list as it did in Drupal 6. To achieve this functionality, you should override theme_field($variables) in your template.php file. Below is an example of how it was achieved for this site:
<?php
function purple_neopolitan_field__field_tags($variables) {
$output = '';
// Subtract 1 from count to match $delta which starts at 0
$count = (count($variables['items']) - 1);
// Render the label, if it's not hidden.
if (!$variables['label_hidden']) {
$output .= '<div class="field-label"' . $variables['title_attributes'] . '>' . $variables['label'] . ': </div>';
}
// Render the items.
$output .= '<div class="field-items"' . $variables['content_attributes'] . '>';
foreach ($variables['items'] as $delta => $item) {
$classes = 'field-item ' . ($delta % 2 ? 'odd' : 'even');
$output .= '<div class="' . $classes . '"' . $variables['item_attributes'][$delta] . '>' . drupal_render($item) . '</div>';
// If this is not the last item, print a comma and space
if($delta < $count) {
$output .= ', ';
}
}
$output .= '</div>';
// Render the top-level DIV.
$output = '<div class="' . $variables['classes'] . '"' . $variables['attributes'] . '>' . $output . '</div>';
return $output;
}
?>Note the theme function I used is for the field named field_tags. I used a theme function as opposed to a template file as there is a small performance gain. Changes to the default theme function include a count of the items:
<?php
// Subtract 1 from count to match $delta which starts at 0
$count = (count($variables['items']) - 1);
?>And a comparative if statement, at the end of the foreach, to check if the current item is the last. If it is not, the function will print a comma and a space.
<?php
// If this is not the last item, print a comma and space
if($delta < $count) {
$output .= ', ';
}
?>Just like that, you are done, and have achieved normalcy.
Resources: drupal.stackexchange.com, Druapl API: theme_image()
RSS
Subscribe to my blog feeds:
Looking for a good way to consume RSS feeds?
Google Reader + Reeder for Mac
Contact
Did you just find a glaring error in this blog? Comments are not enabled yet, so please use the Contact Form to let me know about it.









