Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Nicolas-Bouteille opened this issue Oct 27, 2020 · 0 comments

Comments

@Nicolas-Bouteille
Copy link

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...");
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant