1

I have a field that can accept unlimited values. I want to print those values in my template.

The array that I can see with the devel module looks like this....

Array
(
    [und] => Array
        (
            [0] => Array
                (
                    [value] => Dustin
                    [format] => 
                    [safe_value] => Dustin
                )

            [1] => Array
                (
                    [value] => Jen
                    [format] => 
                    [safe_value] => Jen
                )

            [2] => Array
                (
                    [value] => Mike
                    [format] => 
                    [safe_value] => Mike
                )

        )

)

What I'd like to do is have these values printed to a comma separated list like: Dustin, Jen, Mike

I've been using this code to print single value fields in my template but that of course isn't going to work well with a multi value field..

<?php echo $entityform->field_team_name['und'][0]['value']; ?>
1

field_get_items() will do the trick:

if ( ($items = field_get_items('entityform', $entityform, 'field_team_name')) ) {
  $values = array_map($items, function($x) { return $x['safe_value']; });
  $comma_separated = implode(', ', $values);
}
| improve this answer | |
  • 1
    of course, make sure those ['values'] are safe to just print/echo out to the world :) – Jimajamma Jan 14 '14 at 21:06
  • Thanks for the help. Its not printing out anything though. I'm printing the $comma_separated variable but its empty.. hmm. – Dustin Jan 14 '14 at 21:20
  • Hmmm yeah that should work as far as I can tell...does the condition for field_get_items get entered? If so, what's in $items at that point? – Clive Jan 14 '14 at 21:47
  • Yep, $items spits out an array.. Array ( [0] => Array ( [value] => Dustin [format] => [safe_value] => Dustin ) [1] => Array ( [value] => Jen [format] => [safe_value] => Jen ) [2] => Array ( [value] => Mike [format] => [safe_value] => Mike ) ) – Dustin Jan 14 '14 at 21:55
  • 1
    Thanks Clive. I have it working now. This is what I ended up with... foreach ($items as $item) { $members = $prefix . $item['safe_value']; $prefix = ', '; print $members; } – Dustin Jan 15 '14 at 13:57

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.