Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
migrate_process_array
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
migrate_process_array
Commits
cfd8b2f5
Commit
cfd8b2f5
authored
4 years ago
by
Tess
Browse files
Options
Downloads
Patches
Plain Diff
Added Deepen.
parent
444accd1
Branches
8.x-1.x
Branches containing commit
Tags
8.x-1.3
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/Plugin/migrate/process/Deepen.php
+112
-0
112 additions, 0 deletions
src/Plugin/migrate/process/Deepen.php
src/Plugin/migrate/process/ExtractSingle.php
+6
-0
6 additions, 0 deletions
src/Plugin/migrate/process/ExtractSingle.php
with
118 additions
and
0 deletions
src/Plugin/migrate/process/Deepen.php
0 → 100644
+
112
−
0
View file @
cfd8b2f5
<?php
namespace
Drupal\migrate_process_array\Plugin\migrate\process
;
use
Drupal\migrate\MigrateExecutableInterface
;
use
Drupal\migrate\ProcessPluginBase
;
use
Drupal\migrate\Row
;
/**
* Wraps each item in an array with an array.
*
* Deepen is the opposite of flatten. After processing a field, particularly
* when merging fields together, you may get something like the following:
*
* @code
* array(
* 0 => '123',
* 1 => '456',
* 2 => '789',
* );
* @endcode
*
* Each value is intended as a single field value in a multi-value field, rather
* than as partials of one field. What we need is to reformat the array as such:
*
* @code
* array(
* 0 => array(
* 0 => '123',
* ),
* 1 => array(
* 0 => '456',
* ),
* 2 => array(
* 0 => '789',
* ),
* );
* @endcode
*
* Sometimes we need to set the key for each "deepened" value. We can do so with
* the 'key' item:
*
* @code
* my_field:
* source: my_source
* plugin: deepen
* key: 'target_id'
* @endcode
*
* This would result in output:
*
* @code
* array(
* 0 => array(
* 'target_id' => '123',
* ),
* 1 => array(
* 'target_id' => '456',
* ),
* 2 => array(
* 'target_id' => '789',
* ),
* );
* @endcode
*
* Then we can do run the result through sub_process:
*
* @code
* field_media:
* -
* source: the_source
* plugin: deepen
* key: 'target_id'
* -
* plugin: sub_process
* process:
* target_id: target_id
* @endcode
*
* @MigrateProcessPlugin(
* id = "deepen"
* )
*/
class
Deepen
extends
ProcessPluginBase
{
/**
* {@inheritdoc}
*/
public
function
transform
(
$value
,
MigrateExecutableInterface
$migrate_executable
,
Row
$row
,
$destination_property
)
{
// Only process non-empty values.
if
(
empty
(
$value
))
{
return
NULL
;
}
// The input must be an array.
if
(
!
is_array
(
$value
))
{
$value
=
[
$value
];
}
$out
=
[];
foreach
(
$value
as
$item
)
{
if
(
empty
(
$this
->
configuration
[
'key'
]))
{
$out
[]
=
[
$item
];
}
else
{
$out
[]
=
[
$this
->
configuration
[
'key'
]
=>
$item
];
}
}
return
$out
;
}
}
This diff is collapsed.
Click to expand it.
src/Plugin/migrate/process/ExtractSingle.php
+
6
−
0
View file @
cfd8b2f5
...
...
@@ -9,6 +9,7 @@ use Drupal\migrate\Plugin\migrate\process\Extract;
*
* Normally, a field value is like this:
*
* @code
* array(
* 0 => array(
* 'my_key' => 'The value we want',
...
...
@@ -17,24 +18,29 @@ use Drupal\migrate\Plugin\migrate\process\Extract;
* 'my_key' => 'Another value we want',
* ),
* )
* @endcode
*
* With core's extract, we have can only get the first as we have to specify
* the field delta key.
*
* @code
* my_field:
* source: some_field
* plugin: extract
* index:
* - 0
* - my_key
* @endcode
*
* With this plugin, we can get the value within each field:
*
* @code
* my_field:
* source: some_field
* plugin: extract_single
* index:
* - my_key
* @endcode
*
* @MigrateProcessPlugin(
* id = "extract_single",
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment