Can any one help me how to add comma inbetween the tags in drupal 7.

I have inspect the code for the following tags

Tags: Test bha

the code for this is

         <DIV class="tagsdisplay" >
             <DIV class="label-inline">Tags:&nbsp;</DIV>
                <A  href="http://localhost/drupal/tags/test" typeof="skos:Concept" property="rdfs:label skos:prefLabel">Test</A>
                <A href="http://localhost/drupal/tags/bha" typeof="skos:Concept" property="rdfs:label skos:prefLabel">bha</A>

I have used used the following code to add the comma inbetween the tags

              function fold__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="label-inline"' . $variables['title_attributes'] . '>' . $variables['label'] . ':&nbsp;</div>';

           // Render the items.
          $output .= '<div class="label-inline"' . $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;


Can Anyone help me where i made a mistake.

  • What debugging have you done? Have you tried using dpm($output); (assuming you have the devel module enabled) to see what is being returned by the function (or if it's even called)? Why are there two underscores together in the function name?
    – splatio
    Commented Feb 22, 2012 at 11:26
  • I didn't enable devel module.I have removed the underscores in the function name, still it is not working.Can you help me.I am new to drupal.Can you check the code and tell me the class name which I gave i.e label-inline is correct or not.
    – bharathi
    Commented Feb 22, 2012 at 12:38
  • make sure that $delta is numeric and check whether $variables['items'] is empty or not. Commented Feb 22, 2012 at 12:46
  • I'm not sure the class name would effect things. Is there anything showing up? I would definitely advise installing devel and adding dpm($output); just before you return $output;. This will show you a) the function is being called and b) what is being returned by the function.
    – splatio
    Commented Feb 22, 2012 at 12:50

4 Answers 4


For anyone who needs to do this in the future:

div.field-name-field-tags div.field-items div:after {
  content: ", ";

div.field-name-field-tags div.field-items div:last-child:after {
  content: "";

You can also do this with CSS, without modifying templates or adding modules:

.field-name-field-tags .field-item:after {
    content: ",";
.field-name-field-tags .field-item {
    display: inline;
    margin-right: 0.5em;

The only drawback is that the last one in the list will also have a comma and a space after. On the other hand, there's no messing around with .php or new modules :)


You might also be interested in the module:


This module provides a field display formatter to render field values as HTML or comma-separated lists. This can currently be used on all core field types (text, long text, number, list, and taxonomy reference etc..) and some contributed fields.


I just added the following template to my theme.


<div class="<?php print $classes; ?>"<?php print $attributes; ?>>
  <?php if (!$label_hidden): ?>
    <div class="field-label"<?php print $title_attributes; ?>><?php print $label ?>:&nbsp;</div>
  <?php endif; ?>
  <div class="field-items"<?php print $content_attributes; ?>>
    <?php foreach ($items as $delta => $item) : ?>
      <div style="display:inline;" class="field-item <?php print $delta % 2 ? 'odd' : 'even'; ?>"<?php print $item_attributes[$delta]; ?>>
          print render($item);
          // Add comma if not last item
          if ($delta < (count($items) - 1)) {
            print ', '; 
    <?php endforeach; ?>

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.