Is there a way to format taxonomy term lists to display as a comma separated list? The $variables['#items'] only contain the IDs of the terms, not the terms themselves, and I couldn't find any variable that is available in custom formatters' context and contains the terms. Is there a solution to this?

Comments

troybthompson’s picture

I'm trying to format taxonomy terms without luck either. Did you find a solution?

deciphered’s picture

I would suggest using the Term ID to load the term via taxonomy_term_load().

deciphered’s picture

Status: Active » Fixed

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

capellic’s picture

For those using Drupal 6, taxonomy_term_load() won't work. That's not available until Drupal 7. And if you enter that in and save, you will cause a fatal error on your site that's impossible to correct until you fix the data in your DB.

You need to call taxonomy_get_term() and then refer to the "name":

$term = taxonomy_get_term($element['#item']['value']);
return $term->name;
deciphered’s picture

Thanks capellic.

I answered with the D7 approach because the issue was marked for D7, but I do appreciate the secondary answer for those trying to do the same things in D6.

geek-merlin’s picture

sub

phantomvish’s picture

I can't follow. Can somebody post the formatter's code here. I'm trying to get the content taxonomy terms (links, not just names) as a comma separated list.

StuartDH’s picture

phantomvish’s picture

Thanks StuartDH, but I was looking for a D6 version :-(

lahode’s picture

This code is inspired from http://drupal.org/project/taxonomy_formatter and adapted to this module

Hope it helps someone

$settings = $variables['#display']['settings'];
$element = array();
$separator = ", ";
$formatted = '';
foreach ($variables['#items'] as $delta => $item) {
  $termid = $item['tid'];
  $term[] = $termid;
  $actterm = entity_load('taxonomy_term', $term);
  $uri = entity_uri('taxonomy_term', $actterm[$termid]);
  $formatted .= l($actterm[$termid]->name, $uri['path'], $uri['options']) . $separator;
}
$length = strlen($separator);
$formatted = substr($formatted, 0 , -($length));
$element[0]['#markup'] = $formatted;
return $element;
claudiuv’s picture

Issue summary: View changes

The code at #11 doesnt work as expected. The comma is appended at the end of the list.
Could someone give me a hint to make it work?
Does dpm work inside the php area of the formatter?