Basically if my CCK type has a select field where multiple values are possible, those values swill appear as :

"Value1Value2Value3" in the exported file.

They could be "Value1, Value2, Value 3" or "Value1 Value2 Value3"

Comments

marco.giaco’s picture

I've been facing this problem too, the simplest solution I've found is to add a custom formatter (within a module) and to select this inside field views field format. It would be nice to integrate this inside views_bonus with the ability to choose the preferred separator (now its hardcoded).

/**
* Implementation of hook_theme().
*/

function my_module_theme() {
return array(
'my_module_formatter_separator' => array(
'arguments' => array('element' => NULL),
//'function' => 'theme_my_module_formatter_separator',
),
);
}

/**
* Implementation of hook_field_formatter_info().
*/
function my_module_field_formatter_info() {
return array(
'separator' => array(
'label' => t('Comma separated multiple fields'),
'field types' => array('text'),
'multiple values' => CONTENT_HANDLE_MODULE,
),
);
}

function theme_my_module_formatter_separator($element) {

$values = array();

// Cycle through the file elements
foreach (element_children($element) as $key) {
if (isset($element[$key]['#item']['safe'])) {
$values[] = $element[$key]['#item']['safe'];
}
}

if (empty($values)) {
return;
}

return implode(', ',$values);
}

boabjohn’s picture

Here Here! (+1^10)

It's fairly odd: seems to be an oversight in the usecases for the CSV export. What is the solution to 1000 rows with up to 6 terms concatenated like this? No pattern replace possible...

JB

Sera’s picture

/subscribe

The same error appears when I have a grouped field containing a userreference and a textfield. So my output looks within the CSV export like:
"userreference1userreference2textfield1textfield2"

I dont understand #1 fully. In which file do I have to insert what code snipet?

And yes, it would be cool to choose a separator for that isssue, even a break would be ok to receive

"userreference1
userreference2
textfield1
textfield2"

Best regards
Sera

dgastudio’s picture

+1

liquid06’s picture

+1 If the multivalue fields could be delimited, it seems like this module could be used to move node content out of one Drupal installation and then node import could move it into another Drupal installation. Agree with #1 that the user should choose the separator through the interface - it might need to change based on the content of the field.

nicolas.tala’s picture

+1

This module is really helpfull, but I didn't understand why my users multiples roles were correctly separated, with the separator I chose, and these cck fields were not.

I hope this problem wil be solved soon.

Thanks to all the contributors.

jtjones23’s picture

+1

c4marcus’s picture

Thanks marco.giaco !

However, we were in need of using the separator on a multi select nodereference field. Below is my modified version of your example.

function my_module_theme() {
return array(
'my_module_formatter_separator' => array(
'arguments' => array('element' => NULL),
),
);
}

function my_module_field_formatter_info() {
return array(
'separator' => array(
'label' => t('Comma separated multiple fields'),
'field types' => array('text', 'nodereference'),
'multiple values' => CONTENT_HANDLE_MODULE,
),
);
}

function theme_my_module_formatter_separator($element) {
module_load_include('inc', 'Content', 'includes/content.crud');

$values = array();

// Cycle through the file elements
foreach (element_children($element) as $key) {
if (isset($element[$key]['#item']['safe'])) {
$settings = content_field_instance_read(array(field_name => $element['#field_name']));
if ($settings[0]['type'] == 'text')
$values[] = $element[$key]['#item']['safe'];
else if ($settings[0]['type'] == 'nodereference')
$values[] = $element[$key]['#item']['safe']['title'];
}
}

if (empty($values))
return;

return implode(', ',$values);
}

j d ess’s picture

subscribe

Marco or grizzly, could you provide some elaboration for a noob on how/where/etc the code goes? Thanks!

nruest’s picture

I second j d ess - Marco or grizzly, could you provide some elaboration for a noob on how/where/etc the code goes?

I created a custom module based on grizzly's code and I'm not seeing and difference in the CSV export features at all. A bit more guidance would be great.

Thanks!

Ayesh’s picture

Priority: Normal » Major

+1 - this problem still exists in 6.X 1.11 too(latest to the date).
I think we need to fix this asap.

clintthayer’s picture

And it looks like this is Drupal 7 base code? Not seeing a reference to hook_field_formatter_info for D6.

501cGeek’s picture

I have also encountered this problem, with the latest Drupal 6.20 and Views Bonus Pack 6.x-1.1. Also unclear about what to do with the above code.

As somewhat of a workaround, exporting to XML instead of CSV does separate the multiple entries, by adding HTML divs to separate multiple items in a field. You could potentially do a find and replace on the XML export to replace this HTML with commas. If the intended destination of this export is able to accept XML as input.

mdm’s picture

Hi all,

nruest asked me to post my solution here. All I did was implement hook_preprocess_views_bonus_export_csv in my theme's template.php. Our multi-valued source field looks like this:

[field_format_value] => 
jpg
sid
jp2

and was being output like this:

jpgsidjp2

Which was obviously not what we wanted. My solution was:

function zen_ninesixty_preprocess_views_bonus_export_csv(&$vars) {
  _views_bonus_export_shared_preprocess($vars);

  $vars['separator'] = ",";

  // Special handling when quoted values are involved.
  if ($vars['options']['quote']) {
    $wrap = '"';
    $replace_value = '""';  }
  else {
    $wrap = '';
    $replace_value = '';
  }

  // Format header values.
  foreach ($vars['header'] as $key => $value) {
    $output = decode_entities(strip_tags($value));
    if ($vars['options']['trim']) {
      $output = trim($output);
    }
    $vars['header'][$key] = $wrap . str_replace('"', $replace_value, $output) . $wrap;
  }

  // Format row values.
  foreach ($vars['themed_rows'] as $i => $values) {
    foreach ($values as $j => $value) {
      if ($j === 'field_format_value') {
        $vars['themed_rows'][$i][$j] = $wrap . str_replace('"', $replace_value, decode_entities(strip_tags(rtrim(str_replace("", ",", $value), ',')))) . $wrap;
      } else {
        $vars['themed_rows'][$i][$j] = $wrap . str_replace('"', $replace_value, decode_entities(strip_tags($value))) . $wrap;
      }
    }
  }
}

The important lines here are:

 if ($j === 'field_format_value') {
   $vars['themed_rows'][$i][$j] = $wrap . str_replace('"', $replace_value, decode_entities(strip_tags(rtrim(str_replace("", ",", $value), ',')))) . $wrap;

...which you'll probably have to tweak for your own situation, and of course you should change "zen_ninesixty" in the function name to whatever theme you're using. The $vars['separator'] is hardcoded to a comma here because for whatever reason, this module was ignoring whatever I set in the views admin UI. Hope this helps!

Ayesh’s picture

This issue seems to be fixed in 6.11 release. I tried today with amazing success!

nruest’s picture

@Ayesh what combination of views modules do you have - what are the versions? When I do an export with the 6.x-.1.1 release CCK fields with multiple values are not separated.

---

nvm - got it working now.

aaronbauman’s picture

Version: 6.x-1.0 » 6.x-1.x-dev

this issue persists to 1.x-dev

aaronbauman’s picture

Now that I think about it, however, I think this is primarily an issue with CCK.
CCK field handlers should provide a "separator" option when "group multiple" is selected.
I didn't find such an issue in the CCK queue after a quick look, but I'd be surprised if it has not been posted already.

Encarte’s picture

Subscribing

blauerberg’s picture

Subscribing

dwadson’s picture

I got around this by adding two instances of the field to my view, and used the "Show __ value(s)" and "starting from __" options in "Group multiple values" to put only one value in each field. Since my multi-select field could only have a maximum of 2 values, it was a simple way to split the values.

tobiberlin’s picture

I did implement a module like in #1 - but my problem is that the CCK select box has key values like

1|Option 1
2|Option 2

Now when I activate the module just the keys are exported but not the names/ values. Can somebody help me? How do i get the values like "Option 1" shown in my CSV export instead of the key values?

Best,
Tobias

langworthy’s picture

Here's a first stab at a fix in CCK itself http://drupal.org/node/1829634#comment-6679170