You are here

protected function VboExportCsv::generateOutput in VBO export 8.3

Same name and namespace in other branches
  1. 8.2 src/Plugin/Action/VboExportCsv.php \Drupal\vbo_export\Plugin\Action\VboExportCsv::generateOutput()

Generate output string.

Overrides VboExportBase::generateOutput

File

src/Plugin/Action/VboExportCsv.php, line 43

Class

VboExportCsv
Generates csv.

Namespace

Drupal\vbo_export\Plugin\Action

Code

protected function generateOutput() {
  $config = $this->configuration;
  $header = $this->context['sandbox']['header'];
  $rows = $this
    ->getCurrentRows();

  // Sanitize data.
  foreach ($header as $key => $item) {
    $header[$key] = strtr($item, [
      $config['separator'] => ' ',
    ]);
  }
  $content_replacements = [
    "\r\n" => ' ',
    "\n\r" => ' ',
    "\r" => ' ',
    "\n" => ' ',
    "\t" => ' ',
    $config['separator'] => ' ',
  ];

  // Generate output.
  $csv_rows = [];
  $csv_rows[] = implode($config['separator'], $header);
  foreach ($rows as $row_index => $row) {
    foreach ($row as $cell_key => $cell) {
      $row[$cell_key] = strtr($cell, $content_replacements);
    }
    $csv_rows[] = implode($config['separator'], $row);
    unset($rows[$row_index]);
  }
  $csv_string = implode(PHP_EOL, $csv_rows);
  if (!empty($config['strip_tags'])) {
    $csv_string = strip_tags($csv_string);
  }

  // BOM needs to be added to UTF-8 encoded csv file
  // to make it easier to read by Excel.
  $output = chr(0xef) . chr(0xbb) . chr(0xbf) . (string) $csv_string;
  return $output;
}