I've added some freetagging taxonomies to a content type. Now I would like to create a View and display all tags as a comma-separated list, e.g.:

Music:  Lady Gaga, Michael Jackson, The Beatles
Movies: Avatar, Titanic

I added the taxonomy fields to my View, but they only display the very first value/tag/term (e.g. here: Lady Gaga and Avatar). When I mark the "group similar fields" option, all values/tags are displayed, but not as a comma-separated list, but as follows:

<div class="field-item field-item-0">Lady Gaga</div>
<div class="field-item field-item-1">Michael Jackson</div>
<div class="field-item field-item-2">The Beatles</div>

i.e. listed below each other. Of course, I can user float:left; padding-right:10px; in the divs to display them inline, but it is too hacky and the commas are missing anyways.

Comments

huntsinger’s picture

<?php 

$count = count($terms);
foreach($terms as $term) { 
	print $term;
	  if ( $i < $count-1 ) { print ", "; } $i++; 
	}
?>
merlinofchaos’s picture

Status: Active » Fixed

Is this a CCK field? The field-item tag suggests it is. There should be a 'group multiple values' checkbox that will do that. In any case, it's a CCK request.

jaochoo’s picture

Status: Fixed » Active

Yes, it is a CCK field (taxonomy field) but my question is about how to display this field in a View. I know how to add it to a a View, but:
a) For the 'group multiple values' checkbox I will get an output like mentioned above, i.e. every tag/term produces an own <div> element, i.e. having all tags/terms listed in separate rows rather than getting a comma-separated list of terms;
b) Not checking the 'group multiple values' checkbox I will only get the very first term/tag.

So both are not satisfying.

merlinofchaos’s picture

Status: Active » Closed (won't fix)

Please read the submission guidelines for an explanation of why I say this is a CCK request.

trevorjfitz’s picture

So my friends, any resolution on this? It seems like a views request. Honestly, I don't really care what kind of request it is, I'm just curious how to solve the problem. Huntsinger, where would you suggest putting that code - seeing as we are trying to implement this solution in a view.

Thanks Drupal studs.

jaochoo’s picture

Project: Views (for Drupal 7) » Content Construction Kit (CCK)
Version: 6.x-3.0-alpha3 » 6.x-2.6
Component: taxonomy data » Views Integration
Status: Closed (won't fix) » Active

So as requested I moved this into the CCK queue. I am still interested in a solution. AFAIK same happens with the userreferences. Can someone explain how to display such multiple data (taxonomy terms, user references, node references, etc.) as a comma-separated list, just like OG produces a comma-separated list of groups?

karens’s picture

Project: Content Construction Kit (CCK) » Content Taxonomy
Version: 6.x-2.6 » 6.x-1.x-dev
Component: Views Integration » Code

CCK does not handle that field, I'm guessing you're using Content Taxonomy.

mastoll’s picture

I am not using Content Taxonomy and I have the same question.

Via a view, I'm displaying all of the taxonomy terms assigned to a particular node. Those terms are grouped by vocabulary. They are displayed each in their own div tag, therefore visibly in a list.

How to display them inline, with a coma between each term?

xjm’s picture

Status: Active » Closed (duplicate)

For Content Taxonomy, see: #749432: comma separated, ordered list and unordered list.

You can also accomplish this in the theming layer, using either a theme override or CSS.

For a possible contrib module solution, see also: http://drupal.org/project/custom_formatters

azabeti’s picture

One solution would be to use hook_preprocess_node function in template.php

/*** Create comma separated terms ***/
function phptemplate_preprocess_node(&$vars) {
	$termLinks = array();
	foreach ($vars['taxonomy'] as $term) {
		$termLinks[] = l($term['title'], $term['href']);
	}
	$vars['taxonomy']['term_links'] = implode(', ', $termLinks);
}

Then in node.tpl.php use:
print $variables['taxonomy']['term_links']

There's probable a way to inset into the node object, but I haven't figured that out yet.

lahode’s picture

If it helps someone, here is an export of my formatter with custom_formatter. You can just import it and use it:

$formatter = new stdClass();
$formatter->disabled = FALSE; /* Edit this to true to make a default formatter disabled initially */
$formatter->api_version = 2;
$formatter->name = 'terms_with_comma';
$formatter->label = 'Terms with comma';
$formatter->description = '';
$formatter->mode = 'php';
$formatter->field_types = 'taxonomy_term_reference';
$formatter->code = '$termLinks = array();
if (isset($variables[\'#items\']) && is_array($variables[\'#items\'])) {
foreach ($variables[\'#items\'] as $item) {
$term = taxonomy_term_load($item[\'tid\']);
if ($term) $termLinks[$term->tid] = l($term->name, \'taxonomy/term/\' . $term->tid);
}
}
return implode(\', \', $termLinks);';
$formatter->fapi = '';

Cheers

jwilson3’s picture

This can also be done with a module: https://drupal.org/project/textformatter

jospratiklive’s picture

I did tried the 'textformatter' module, it does not work for 'Content Taxonomy Fields' type cck field.

pringlz’s picture