Skip to content

Suggestion: make sure the row counter reflects the correct number of lines we are trying to import #17

@Nicolas-Bouteille

Description

@Nicolas-Bouteille

I was testing with a csv file of two members to import, so 1 header line and 2 interesting lines.
I noticed the @ToTal was displaying 4 instead of 2 lines, and the @current row was ahead of one line.
I noticed that in order to remember the filename you add an operation to the batch, so this step is actually taken into account in the @ToTal counter.
So instead of doing it in a separated operation, I pass the filename as an extra parameter to the ImportLine operation and do it the first time only:

$batch['operations'][] = [
  '\Drupal\csvimport\Batch\CsvImportBatch::csvimportImportLine',
  [array_map('base64_encode', $line), $csvupload],
];
public static function csvimportImportLine($line, $filename, &$context) {
  if ($context['results']['rows_imported'] == 0) {
    $context['results']['uploaded_filename'] = $filename;
  }
...

I also realized that you don't skip the first line (headers) when adding operations to the batch, which is why there is still one too many... so here's my solution:

        $lines = [];
        $skip_first_line = TRUE;
        while ($line = fgetcsv($handle, 4096)) {
          if ($skip_first_line) {
            $skip_first_line = FALSE;
            continue;
          }
          $lines[] = $line;
        }
        $nb_lines = count($lines);
        foreach ($lines as $line) {
          // Use base64_encode to ensure we don't overload the batch
          // processor by stuffing complex objects into it.
          $batch['operations'][] = [
            '\Drupal\csvimport\Batch\CsvImportBatch::csvimportImportLine',
            [array_map('base64_encode', $line), $csvupload, $nb_lines],
          ];
        }

As you can see, I also pass the total number of lines to the operation. This allows to change the last message to "Finalizing..." instead of "Importing line 3" when there is only 2 of them...

$context['results']['rows_imported']++;
    if ($context['results']['rows_imported'] < $total_lines) {
      // This message will be displayed while next row is treated, so we add +1
      $context['message'] = t('Importing row @row', ['@row' => $context['results']['rows_imported'] + 1]);
    }
    else {
      $context['message'] = t("Finalizing...");
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions