Use a delimiter for multiple valued CCK fields in CSV

alex.k - January 29, 2009 - 13:10
Project:Views Bonus Pack
Version:6.x-1.0-beta4
Component:Code
Category:bug report
Priority:normal
Assigned:Unassigned
Status:active
Description

It may be the same issue as #338258: CSV exporting of multiple values in fields reported some time ago for 5.x

If there is a multiple-valued CCK text field with values like English, German, French, etc., it will be exported to CSV as EnglishGermanFrench. Would it be possible to add a delimiter like with taxonomy fields? Configurable delimiter would be nice, but just comma-separated is good as well.

Thanks for these excellent features.

#1

alex.k - January 29, 2009 - 13:17

It is probably directly related to how Views formats these fields. Taxonomy does use a configurable delimiter, whereas multi-valued CCK fields would be rendered with < ul>< li>value< /li>.... - so markup is stripped and we end up with just text in CSV.

#2

neclimdul - January 29, 2009 - 18:05

That explains a lot. I was never sure what that issue was trying to do.

I'll see if there's something we can do.

#3

amanire - May 13, 2009 - 01:12

Has there been any progress on this issue? Currently, it is concatenating values without any separation.

#4

amanire - May 13, 2009 - 18:20
Category:feature request» bug report

I am recategorizing this as a bug, since concatenating values without any separation renders the output difficult to read and impossible to analyze in an application like Excel.

#5

servantleader - July 6, 2009 - 17:14
Version:6.x-1.0-beta1» 6.x-1.0-beta4

Has anyone found a work around for this issue or anything that will work?

#6

gd1008 - July 22, 2009 - 23:07

Subscribing

#7

Ryan Aslett - July 28, 2009 - 17:48

Subscribing

#8

dieter - August 21, 2009 - 09:58

hi, I fixed this in D6 as follows (probably not the cleanest way to do this, but it works)

function template_preprocess_views_bonus_export_csv(&$vars) {
  drupal_set_header('Content-type: text/csv; charset=utf-8');

  // TODO Replace items with themed_rows.
  _views_bonus_export_shared_preprocess($vars);

  if ($vars['options']['quote']) {
    $vars['seperator'] = '","';
    $replace_value = '""';
  }
  else {
    $vars['seperator'] = ',';
    $replace_value = '';
  }

  foreach ($vars['themed_rows'] as $i => $values) {
    foreach ($values as $j => $value) {
          // START CHANGES DIETER
          $value = str_replace('</div>', '||', $value);
          if (substr_count($value, '||') < 2) {
             // get rid of superfluous separators
             $value = str_replace('||', '', $value);
          } else { //remove trailing separator
             $value = substr($value, 0, strlen($value)-2);
          }
          // END CHANGES DIETER

      $vars['themed_rows'][$i][$j] = str_replace('"', $replace_value, decode_entities(strip_tags($value)));
    }
  }
}

#9

Branndon - August 22, 2009 - 21:06

Where do I paste that?

#10

dieter - August 24, 2009 - 09:41

Just replace the function in modules/views_bonus/ export/views_bonus_export.module

#11

neclimdul - August 26, 2009 - 04:35

I'm trying to work something out for this along with #552172: Accept fields that render XML node structure.

#12

sayuri - November 16, 2009 - 04:19

Thanks so much for that solution, dieter. It works perfectly for me in D5.18 with Views Bonus v5.x-1.2-alpha2. In that case, you need to insert the code in modules/views_bonus/views_bonus_export.module as below:

/**
*  Main Function to export a view as CSV
*/
function theme_views_bonus_export_csv($view, $nodes) {

...

  // one row for each node
  foreach ($nodes as $node) {
    $values = array();
    foreach ($view->field as $field) {
      if ($fields[$field['id']]['visible'] !== false) {
        $value = views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $node, $view);
       
        // START CHANGES DIETER
         $value = str_replace('</div>', '||', $value);
         if (substr_count($value, '||') < 2) {
           // get rid of superfluous separators
           $value = str_replace('||', '', $value);
         } else { //remove trailing separator
             $value = substr($value, 0, strlen($value)-2);
         }
        // END CHANGES DIETER
       
        $values[] = '"' . str_replace('"', '""', decode_entities(strip_tags($value))) . '"';
      }
    }
    print implode($comma, $values) ."\r\n";
  }
  module_invoke_all('exit');
  exit;
}

 
 

Drupal is a registered trademark of Dries Buytaert.