You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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...");
}
The text was updated successfully, but these errors were encountered:
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:
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:
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...
The text was updated successfully, but these errors were encountered: