I've used this module to create a comma separate list for fields where we have a multi-select. The one thing I can't figure out is how to tell it not to show the comma after the last item.

Right now we get this:

Label: Item1, Item2, Item3, Item4,

We want:

Label: Item1, Item2, Item3, Item4

Comments

SaxxIng’s picture

There are several possibilities. Try this:

$output = NULL;
foreach (element_children($element) as $key) {
  $output .= ($output==NULL) ? '' : ', ';
  if ($element[$key]['#item']) {
    $output .= $element[$key]['#item']['value'];
  }
}
return $output;
jsimonis’s picture

Status: Active » Closed (fixed)

Perfect! That worked great.

I think it would be great to have a recipe book on here for things like this, contemplate, views, etc.

todd zebert’s picture

almost exactly what I'm looking for, but how do I incorporate something like http://drupal.org/node/700480 to get the Value for the key?

todd zebert’s picture

I couldn't figure out my last question (still would like to know) but since I only had 1 key!=value I used a quick compare statement.

Anyway, I wanted a "collapsed" list of values using dashes for ranges, and commas. This works:

$output = NULL;
$last = NULL;
$punc = NULL;
foreach (element_children($element) as $key) {
  if ($output==NULL) {
    $output .= ($element[$key]['#item']['value']==0) ? 'K' : $element[$key]['#item']['value'];
    $last = $element[$key]['#item']['value'];
  } elseif ($element[$key]['#item']['value']==($last+1)) {
    //$output .= '(' . $element[$key]['#item']['value'] . ')';
    $last = $element[$key]['#item']['value'];
    $punc = '-';
  } else {
    if ($punc) { $output.= $punc  . $last; }
    $output .= ', ' . $element[$key]['#item']['value'];
    $last = $element[$key]['#item']['value'];
    $punc = NULL;
  }
}

if ($punc) { $output .= $punc . $last; }

return $output;

There's probably a more elegant solution but it works.

todd zebert’s picture

Oddly enough my code works well in the regular cck display in a node, but from a view two things happened:

1) each individual element would print K,1,2,3,4,5 in order across subsequent rows. I ensured my Formatter is selected. Oddly, I have to check "Group multiple values" and it uses the formatter (or perhaps it's doing it itself, not sure).

2) Now it correctly outputs as my formatter "K-5" BUT it uses

tags so breaks the inline from the content. All the other fields use (not
)and if I change the code in the browser to it works as expected. I haven't found a solution to this yet.

Thanks!