Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
M
migrate_source_csv
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Custom Issue Tracker
Custom Issue Tracker
Labels
Merge Requests
0
Merge Requests
0
Requirements
Requirements
List
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Analytics
Analytics
Code Review
Insights
Issue
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
project
migrate_source_csv
Commits
95530e96
Commit
95530e96
authored
May 22, 2020
by
csmdgl
Committed by
heddn
May 22, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue
#2913426
by csmdgl, pcambra, DuaelFr, heddn: Add cvsrownum to the source CSVFileObject
parent
3ea0bc85
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
75 additions
and
1 deletion
+75
-1
config/schema/migrate_source_csv.source.schema.yml
config/schema/migrate_source_csv.source.schema.yml
+6
-0
src/Plugin/migrate/source/CSV.php
src/Plugin/migrate/source/CSV.php
+15
-0
tests/src/Unit/Plugin/migrate/source/CSVUnitTest.php
tests/src/Unit/Plugin/migrate/source/CSVUnitTest.php
+54
-1
No files found.
config/schema/migrate_source_csv.source.schema.yml
View file @
95530e96
...
...
@@ -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'
src/Plugin/migrate/source/CSV.php
View file @
95530e96
...
...
@@ -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
;
}
}
...
...
tests/src/Unit/Plugin/migrate/source/CSVUnitTest.php
View file @
95530e96
...
...
@@ -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
;
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment