I have multiple fields that have multiple values and I would like the ability to put a separator between them. For example here is what is happening right now.

Field: What are your favorite colors? [X]blue []green [X]red []yellow
Output: Favorite colors: blue green

What I would like the output to be is: Favorite colors: blue, green

Maybe there is already a good easy way to do this (I've already tried to look all over the place to no avail) and if somebody could tell me what it is that would be awesome but I really think there should be a place under "Group multiple values" where you can enter a separator.

Thanks!

Comments

dawehner’s picture

Category: feature » support
Status: Active » Fixed

So you have two fields. You can use the "rewrite output of this field" setting to do it.

In the second field use this rewrite pattern:

[field1] [field2]

In the first field use this rewrite pattern:

[field1] ,
deggertsen’s picture

Status: Fixed » Active

I'm aware of how to do that. But the problem is that it's only one field with multiple values that I'm using. So I'm wondering if there is a way to rewrite the output of [field1]'s multiple values. Because field 1 contains all of the values (i.e. blue green red and yellow). Does that make sense? If each value was contained in separate field it would be easy to comma separate them but since that is not the case what do I do? I believe there is a way to create a tpl.php file to change the output of a specific field but I don't really understand how I could use that method to add separators. And it would just be nice to have an option under "Group multiple values" that allows you to insert a separator.

ezeedub’s picture

I'd like to see this improvment also. I was surprised that it wasn't there when I first encountered this feature, and then I forgot about it.

Using n fields is not practical. You'd have to set n "starting from" offsets for fields with n values.

Anyway, selecting the same field multiple times and rewriting does not solve for fields that allow unlimited values.

jay-dee-ess’s picture

subscribing

jcarlson34’s picture

Not sure but if you are able to find a way to get the values into separate fields, this http://drupal.org/node/854002 might help you hide the commas for the ones that aren't active.

merlinofchaos’s picture

Status: Active » Closed (won't fix)

If you're talking about a CCK field, it's up to CCK to provide the separator in the handler. Most multi-value fields for core, Views already provides this functionality.

roball’s picture

You can do this by copying "views-view-field.tpl.php" into your theme's directory, under a name the view will recognize it selectively. Put the following code in it:
print preg_replace('/<\/div><div[^>]+>/i', ', ', $output);

deggertsen’s picture

Thank you roball. That is very helpful!

GRRaka’s picture

I have the same requirements, but my data is being exported to a spreadsheet.
Using Group multiple values results in "redgreenblue".
How can I make this look like "red,green,blue"?

GRRaka’s picture

#7 worked for me.
Thanks roball.

baisong’s picture

I had the same problem, creating a downloadable CSV feed view for a view, all my data was being concatenated with no separator -- I believe if it wasn't a plain-text view handler, there would be markup to separate the values.

As a solution, I used a customfield PHP snippet to generate arrays and implode them in the view. Probably slow for production sites, but for data extraction, it seemed sufficiently fast. Just install the customfield module, add a customfield PHP snippet (make sure you have permission to evaluate the PHP input format) and then paste these snippets in (along with the unseparated multivalued fields, exclude to hide them) to generate the separated list.

This is for text/number values

<?php
/**
 * make a list of all values in a multivalued field
 * 
 * load the value for each value of the multivalued field, 
 * put all of the values into an array, and then implode the
 * list for comma-separated display.
 *
 * to fish for more available variables, use `print_r($data);`
 */
$output = array();
foreach ($data->node_data_field_my_text_field_my_text_value as $my_text) {
    if ($my_text['value']) {
        $output[] = $my_text['value'];
    }
}
print_r(implode($output,","));
?>

This is for node references (I needed the title from the node id (NID)

<?php
/**
 * make a list of titles of a referenced node field
 *
 * load the NID for each referenced node, then load the node
 * using the Drupal API, and create an array of titles from
 * the node object, then implode the list for comma-separated display.
 *
 * to fish for more available variables, use `print_r($data);`
 */
$output = array();
foreach ($data->node_data_field_my_noderef_field_my_noderef_nid as $my_nid) {
    if ($my_nid['nid']) {
       $my_node = node_load($my_nid['nid']);
       $output[] = $my_node->title;
    }
}
print_r(implode($output,","));
?>
ohthehugemanatee’s picture

#7 is a great, elegant way to handle the problem. Thanks!

aubjr_drupal’s picture

Thanks for #7, roball! Elegant and simple solution...

Daniel Wentsch’s picture

Yepp, thanks roball, now using #7, too!

manila555’s picture

Thanks roball!

#7 worked like a charm.

serialbob’s picture

#7 worked partially, the first and the end div stayed alive :-(

gejo’s picture

Thanks, roball!
Your solution in #7 worked like a charm for me.

stephesk8s’s picture

I also needed to export multiple node reference titles from a feeds View to a .csv file. I'm migrating our content from our current D6 site to our new D7 site, so I need to be able to explode the list with Feeds Tamper on the other end, but found nothing to add a separator to the grouped multiple values.

My field was set up as a Content: Node Reference field, (in my case "Content: Related Documents (field_related_documents)") and the Group Multiple Values was checked. The formatter was Title (no link).

By default, the titles are exported with no separator - i.e "Title OneTitle TwoTitle Three".

I had already been using the Custom Formatter module to grab the database key value for other field imports, so I set up a custom formatter for my Node Reference field with the [title-raw] token and a separator (in my case, I've had good luck with >> exploding well in Feeds Tamper so for this formatter it was [title-raw]>>).

Then in my Views display I was able to choose my new custom formatter for the format of my node reference field and the separator was added. Voila :)

(My custom formatter was very basic, so I had to find and replace away the last extra set of >>'s in my final .csv file in my text editor. I'm sure there is a more elegant way set up the formatter, but for me, this worked for my one time export. Also my D6 site is using Views 2 in case that makes a difference here.)

styledata@yahoo.com’s picture

#7 works GREAT!

wooody’s picture

#7 Thank you roball ...

Vali Hutchison’s picture

Issue summary: View changes

Can also do it via CSS, for example:

.view-MyViewName .group div.field-item:before {
  content:", ";
}
garbo’s picture

vrajak@gmail.com’s picture

*edit* Can't seem to delete this post, but I figured it out so need for it.

squarecandy’s picture

+1 for using Custom Formatters module for CSV export issues (#18).

Install https://www.drupal.org/project/custom_formatters

Add a new formatter admin/build/formatters/add

I used the Basic type of formatter with the field type text and entered in the HTML field:

[raw]|

(I used the pipe character here, but you can use whatever unique separator you want).

Then go in and edit the view, edit the field in question with multi values, select your custom text format under the format drop down.

I know this thread is old, but hopefully this will help someone.

zkrebs’s picture

Thanks #24 squarecandy,

Used this for an old D6 migration and it worked beautifully.

gumrol’s picture

+1 for #18 and #24

I wanted no trailing delimiter, so opted for the Advanced formatter type and did the following:

$a_items = array();
foreach (element_children($element) as $key) {
  $a_items[] = trim($element[$key]['#item']['value']);
}
return implode(',', $a_items);
frederickjh’s picture

@gumrol Thanks for your code in #26!

It is working for a number of fields.

However, it is not working for email address fields. I think it is the @ in the email addresses.

Anyone know how to handle email address correctly for a custom formater?

Thanks!
Frederick

frederickjh’s picture

@gumrol Oh, forgot to say with your custom formater code I get the delimiters but no email addresses.

frederickjh’s picture

Delimiting custom formatters for Links and Emails require a change to the code above in comment #26:

Replace this line:
$a_items[] = trim($element[$key]['#item']['value']);

For links:
$a_items[] = trim($element[$key]['#item']['url']);

For email addresses:
$a_items[] = trim($element[$key]['#item']['email']);