When using Views Data Export, and the `$data` array contains only a message, the following error is thrown:

`TypeError: Argument 1 passed to League\Csv\Writer::insertOne() must be of the type array, string given, called in /var/www/argonne-insideout.local/docroot/modules/contrib/csv_serialization/src/Encoder/CsvEncoder.php on line 158 in League\Csv\Writer->insertOne() (line 151 of /var/www/argonne-insideout.local/vendor/league/csv/src/Writer.php).`

This is related to #2975802: Undefined offset: 0 in Drupal\csv_serialization\Encoder\CsvEncoder->extractHeaders(), and a similar patch was used to fix this error. However, it seems like this condition should be checked before attempting to encode to CSV.

Comments

byrond created an issue. See original summary.

byrond’s picture

StatusFileSize
new563 bytes
steinmb’s picture

Status: Active » Needs review
Bagz’s picture

Came across this same issue on 8.x-2.0-beta3, thanks for the patch.

tjtj’s picture

This is also happening in 9.1.4.
Message TypeError: Argument 1 passed to League\Csv\Writer::insertOne() must be of the type array, string given, called in /home/briarcli/public_html/web/modules/contrib/csv_serialization/src/Encoder/CsvEncoder.php on line 159 in League\Csv\Writer->insertOne() (line 140 of /home/briarcli/public_html/vendor/league/csv/src/Writer.php)
Does this patch work?

tjtj’s picture

The patch does work in 9.1.4. Why isn't it incorrporated in the distro? People spand a lot of time chasing down patches!

grasmash’s picture

Status: Needs review » Fixed

  • grasmash committed 339a4e9 on 8.x-2.x authored by byrond
    Issue #3120645 by byrond: TypeError in CsvEncoder.php
    
acbramley’s picture

Status: Fixed » Needs work
+++ b/src/Encoder/CsvEncoder.php
@@ -155,8 +155,10 @@ class CsvEncoder implements EncoderInterface, DecoderInterface {
+      if (isset($data[0])) {

This will still be true if $data is a string

>>> $data = 'test123';
=> "test123"
>>> isset($data[0]);
=> true
acbramley’s picture

Status: Needs work » Needs review
StatusFileSize
new1.05 KB
new1.56 KB

The bug wasn't when $data wasn't an array (that's actually taken care of at the top of the function), but when $row wasn't. This patch fixes it with a test, however automated tests need to be set up for 2.x and the decoding test seems to be failing.

steinmb’s picture

Status: Needs review » Reviewed & tested by the community

I think your right. I also noticed in CsvEncoder::flattenCell contain a call similar to this.

  protected function flattenCell(array $data) {
    $depth = $this->arrayDepth($data);

    if ($depth == 1) {
      // @todo Allow customization of this in-cell separator.
      return implode('|', $data);
    }

      $cell_value = "";
      foreach ($data as $item) {
        $cell_value .= '|' . $this->flattenCell($item);
      }

      return trim($cell_value, '|');
  }

Line $cell_value .= '|' . $this->flattenCell($item); it calls it self, but when the array you unpack contain a string item, booom!

  • grasmash committed 4295116 on 8.x-2.x authored by acbramley
    Issue #3120645 by acbramley, byrond, grasmash: TypeError in CsvEncoder....
grasmash’s picture

Status: Reviewed & tested by the community » Fixed

  • grasmash committed fccc64d on 3.x authored by acbramley
    Issue #3120645 by acbramley, byrond, grasmash: TypeError in CsvEncoder....

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.