10

Drupal 7.

In my node.tpl.php, I want to print out a list of taxonomy terms (the taxonomy is called "channels."). If I use:

<?php print render($content['field_channel']); ?>

It works, obviously, but the best I can do to get them inline is to use CSS to float them left. I would like them to be separated by commas. Any ideas?

Thx.

11

You could try theming the field using field.tpl.php or theme_field().

As an example (using field.tpl.php):

  1. Copy field.tpl.php to your theme directory from "modules/field/theme"
  2. Make a copy of that file and rename it to field--field-channel.tpl.php
  3. Edit the file however you like.

As a quick/dirty example for this to work, field--field-channel.tpl.php could look like:

<div class="<?php print $classes; ?> clearfix"<?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]; ?>>
        <?php 
          print render($item);
          // Add comma if not last item
          if ($delta < (count($items) - 1)) {
            print ','; 
          }
        ?>
      </div>
    <?php endforeach; ?>
  </div>
</div>

There are probably multiple ways to accomplish this using the .tpl file, but this is just one option. I recommend adding a class to the DIV instead of a style and make the changes in your stylesheet instead of using inline styles.

  • Awesome, this worked perfectly. I didn't know you could use templates on the fields like that. Still have a lot to learn with this stuff. Thanks again. – Mike May 23 '11 at 20:09
  • Using tpl files is a bit more resource intense and the preferred mode is to use the hook_field suggestion below. – shaneonabike Dec 13 '14 at 4:22
12

The Text Formatter module is now available for Drupal 7, and lets you do this without custom theme work.

  • Text Formatter module is probably the ideal solution to this now, as opposed to the chosen answer above which requires mucking around in the theme layer. – JamesWilson Jul 15 '13 at 22:09
7

Here's one way to use the theme_field approach (add it to your template.php file):

/**
 * Implements theme_field()
 *
 * Make field items a comma separated unordered list
 */
function THEMENAME_field__NAME_OF_FIELD__NAME_OF_CONTENT_TYPE($variables) {
  $output = '';

  // Render the label, if it's not hidden.
  if (!$variables['label_hidden']) {
    $output .= '<div class="field-label"' . $variables['title_attributes'] . '>' . $variables['label'] . ':&nbsp;</div>';
  }

  // Render the items as a comma separated inline list
  $output .= '<ul class="field-items"' . $variables['content_attributes'] . '>';
  for ($i=0; $i < count($variables['items']); $i++) {
    $output .= '<li>'. drupal_render($variables['items'][$i]);
    $output .= ($i == count($variables['items'])-1) ? '</li>' : ', </li>';
  }
  $output .= '</ul>';

  return $output;
}
  • 1
    Super great thanks this helped a lot actually! Also the preferred method then a tpl file! – shaneonabike Dec 13 '14 at 4:22
4

You can do this easily just in CSS:

.field-type-taxonomy-term-reference .field-items .field-item {
  display: inline-block;
  *display: inline;
  *zoom: 1;
}
.field-type-taxonomy-term-reference .field-items .field-item:after {
  content: ", ";
}
.field-type-taxonomy-term-reference .field-items .field-item:last-child:after {
  content: "";
}
2

Use a preprocess function: http://planken.org/2009/08/03/create-comma-separated-taxonomy-term-list-drupal

  • Sorry this is for Drupal 6, just realized this is a D7 post – ipwa Jul 20 '11 at 0:41
0
<?php
if ($node->taxonomy) {
    foreach($node->taxonomy as $term) {
        if ($term->vid == 3) { // the id of the vocabulary
            $my_terms[] = l(
                t($term->name),
                'taxonomy/term/' . $term->tid
            );
        }
    }
}

if ($my_terms) { ?>
    <div class="clear-block">
        <div class="terms">
            <?php print implode(", ", $my_terms); ?>
        </div>
    </div>
<?php } ?>
  • 3
    This is sidestepping the theme system, putting hard coded display logic into the tpl file and creating a maintenance headache. – Jeremy French May 21 '11 at 13:11
  • Is it better to use side modules which are doing almost the same? Could you please provide a more maintenance-friendly code? – Rootical V. Feb 4 '13 at 14:33
0

Even easier for delimiter and the wrapper, you use the Taxonomy Formatter module: http://drupal.org/project/taxonomy_formatter

More details from project page:

This is a small module written to provide a custom formatter for taxonomy items. The default formatters both output the terms wrapped in divs. This module adds a new formatter that allows you to specify the element type, the wrapper type, classes for both, the separator used, and if they link to the term pages or not. This gives much more customizable layout options.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.