Commit 95530e96 authored by csmdgl's avatar csmdgl Committed by heddn

Issue #2913426 by csmdgl, pcambra, DuaelFr, heddn: Add cvsrownum to the source CSVFileObject

parent 3ea0bc85
......@@ -32,3 +32,9 @@ migrate_plus.source.csv:
file_flags:
type: integer
label: 'Bitmask flags for the SplFileObject'
create_record_num:
type: boolean
label: 'Specifies whether to create an incremented value for each record in the file'
record_num_field:
type: string
label: 'Field name for record number field'
......@@ -27,6 +27,10 @@ use League\Csv\Reader;
* Defaults to double quote marks.
* - escape: (optional) The field escape character (one character only).
* Defaults to a backslash (\).
* - create_record_number: (optional) Boolean value specifying whether to create
* an incremented value for each record in the file. Defaults to FALSE.
* - record_number_field: (optional) The name of a field that holds an
* incremented value for each record in the file. Defaults to record_num.
*
* @codingStandardsIgnoreStart
*
......@@ -130,6 +134,11 @@ class CSV extends SourcePluginBase implements ConfigurableInterface {
}
}
}
// If "create_record_number" is specified, "record_number_field" must be a
// non-empty string.
if ($this->configuration['create_record_number'] && (!is_scalar($this->configuration['record_number_field']) || (empty($this->configuration['record_number_field'])))) {
throw new \InvalidArgumentException('The configuration "record_number_field" must be a non-empty string.');
}
}
/**
......@@ -144,6 +153,8 @@ class CSV extends SourcePluginBase implements ConfigurableInterface {
'delimiter' => ",",
'enclosure' => "\"",
'escape' => "\\",
'create_record_number' => FALSE,
'record_number_field' => 'record_number',
];
}
......@@ -229,7 +240,11 @@ class CSV extends SourcePluginBase implements ConfigurableInterface {
* @codingStandardsIgnoreEnd
*/
protected function getGenerator(\Iterator $records) {
$record_num = $this->configuration['header_offset'] ?? 0;
foreach ($records as $record) {
if ($this->configuration['create_record_number']) {
$record[$this->configuration['record_number_field']] = ++$record_num;
}
yield $record;
}
}
......
......@@ -6,7 +6,6 @@ use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate_source_csv\Plugin\migrate\source\CSV;
use Drupal\Tests\UnitTestCase;
use org\bovigo\vfs\vfsStream;
use PHPUnit\Framework\Error\Warning;
/**
* @coversDefaultClass \Drupal\migrate_source_csv\Plugin\migrate\source\CSV
......@@ -245,6 +244,60 @@ EOD;
],
],
];
$data['default record number field name'] = [
'configuration' => [
'ids' => ['id'],
'create_record_number' => TRUE,
],
'expected rows' => [
[
'id' => '1',
'first_name' => 'Justin',
'last_name' => 'Dean',
'email' => 'jdean0@example.com',
'country' => 'Indonesia',
'ip_address' => '60.242.130.40',
'record_number' => 1,
],
[
'id' => '2',
'first_name' => 'Joan',
'last_name' => 'Jordan',
'email' => 'jjordan1@example.com',
'country' => 'Thailand',
'ip_address' => '137.230.209.171',
'record_number' => 2,
],
],
];
$data['custom record number field name'] = [
'configuration' => [
'ids' => ['MyRowNumber'],
'create_record_number' => TRUE,
'record_number_field' => 'MyRowNumber',
'header_offset' => 1,
],
'expected rows' => [
[
'1' => 'id',
'Justin' => 'first_name',
'Dean' => 'last_name',
'jdean0@example.com' => 'email',
'Indonesia' => 'country',
'60.242.130.40' => 'ip_address',
'MyRowNumber' => 2,
],
[
'1' => '2',
'Justin' => 'Joan',
'Dean' => 'Jordan',
'jdean0@example.com' => 'jjordan1@example.com',
'Indonesia' => 'Thailand',
'60.242.130.40' => '137.230.209.171',
'MyRowNumber' => 3,
],
],
];
return $data;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment