CSV export generates invalid CSV when values contain HTML " entity

xurizaemon - October 9, 2008 - 01:01
Project:Views Bonus Pack
Version:5.x-1.2-alpha2
Component:Views Export
Category:bug report
Priority:normal
Assigned:Unassigned
Status:duplicate
Description

CSV exports where the node value contains an '"' (here's hoping that came through OK; it's the HTML quote entity if not) will be invalid because an extra " will get inserted into $value by decode_entities() after the quotes have been handled. This happens in theme_views_bonus_export_csv()

This is most often seen when another module is automatically encoding content, eg with TinyMCE or some other richtext editor.

A quick fix which doesn't require hacking the module is to add this to your template.php - it will override the module function:

/**
*  Override views_bonus function to export a view as CSV
*/
function phptemplate_views_bonus_export_csv($view, $nodes) {
  if (!user_access('export views')) {
    return;
  }
  $fields = _views_get_fields();

  // headings row
  $headings = array();
  foreach ($view->field as $field) {
    if ($fields[$field['id']]['visible'] !== false) {
      $headings[] = $field['label'] ? $field['label'] : $fields[$field['fullname']]['name'];
    }
  }
  $output .= implode(',', $headings) ."\r\n";

  // one row for each node
  foreach ($nodes as $node) {
    $values = array();
    foreach ($view->field as $field) {
      if ($fields[$field['id']]['visible'] !== false) {
        $value = $field;
        $value = views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $node, $view);
        $value = preg_replace('/<.*?>/', '', $value); // strip html tags
        $value = str_replace(array("\r", "\n", ','), ' ', $value); // strip line breaks and commas
        $value = str_replace(array('"','&quot;'), array('""','""'), $value); // escape " characters
        $value = decode_entities($value);
        $values[] = '"' . $value . '"';
      }
    }
    $output .= implode(',', $values) . "\r\n";
  }
  drupal_set_header('Content-Type: text/x-comma-separated-values');
  drupal_set_header('Content-Disposition: attachment; filename="view-'. $view->name .'.csv"');
  print $output;
  module_invoke_all('exit');
  exit;
}

A patch (makes same modification) will be attached in a moment.

#1

xurizaemon - October 9, 2008 - 01:02
Status:active» needs review
AttachmentSize
318813.patch 709 bytes

#2

xurizaemon - October 9, 2008 - 01:03

So, the " in the first line of the above report should read '&quot;'. Yay for input filters :)

#3

neclimdul - November 24, 2008 - 17:15
Status:needs review» duplicate

I'm pretty sure this is a duplicate of #238465: CSV Export: Field encoding sequence

Feel free to reopen if this isn't fixed in the dev release.

 
 

Drupal is a registered trademark of Dries Buytaert.