- Issue created by @benjifisher
- πΊπΈUnited States benjifisher Boston area
In #3236774-9: Provide ability to reference current value of process pipeline as a source property β , @danflanagan8 pointed out that passing
null
as part of thesource
array of a process plugin has the effect of inserting the pipeline value.At π [meeting] Migrate Meeting 2024-03-29 Active , @mikelutz said that this behavior is a bug, and we should avoid using it.
The process plugin proposed here is more flexible, and it makes the intention clearer.
I think this process plugin is also more flexible than the one proposed in β¨ Add wrapper process plugin to wrap/unwrap values in arrays Needs review .
- Status changed to Needs work
8 months ago 7:05pm 14 April 2024 - πΊπΈUnited States benjifisher Boston area
Some more examples, from the doc block:
Generic example:
process: bar: plugin: build_array source: foo template: key: literal string properties: - source:field_body/0/value - dest:field_body/0/value - pipeline:some/nested/key
Prepare an entity reference revision (ERR) field
process: field_paragraph: - plugin: migration_lookup # ... - plugin: build_array template: target_id: pipeline:0 target_revision_id: pipeline:1
Here is an example from my current project.
Create a serialized array for the
layout_paragraphs
moduleprocess: behavior_settings: # ... - plugin: build_array template: layout_paragraphs: parent_uuid: pipeline:0/value region: first - plugin: callback callable: serialize
- πΊπΈUnited States danflanagan8 St. Louis, US
Very excited to see this, @benjifisher! I haven't reviewed the code yet, but my first impression on the name is that we're dangerously close to the core
array_build
plugin! The first best replacement name that jumps out at me would bearray_template
. - Status changed to Needs review
8 months ago 9:35pm 20 April 2024 - πΊπΈUnited States benjifisher Boston area
@danflanagan8:
Thanks for taking a look!
I added some tests, starting with yours from β¨ Add wrapper process plugin to wrap/unwrap values in arrays Needs review . That way,
- Your excellent test coverage does not go to waste if we decide to close that issue in favor of this one.
- I confirm that this plugin can do anything the
wrap
plugin can do (withmethod: wrap
).
When I first wrote this plugin, I called it
array_build
, but then I realized the problem. I confess I was not very creative when I turned it intobuild_array
, and you are right about the potential for confusion.I can go with
array_template
, but other variations are worth considering:array_template
template_array
template
Or maybe the name should indicate that it can work with source, destination, and pipeline values.
How do you feel about using the short form "dest" instead of spelling out "destination"?
- Issue was unassigned.
- πΊπΈUnited States benjifisher Boston area
I changed the plugin ID to
array_template
, and I changed the class names (plugin and test classes) to match. - πΊπΈUnited States benjifisher Boston area
Here is a more complicated example from my current project. A custom source plugin provides the source field
filters
that looks something like this:[ [ 'vid' => 'some_vocab', 'tids' => [1, 2, 3, 5], ], [ 'vid' => 'another_vocab', 'tids' => [8, 13, 21], ], ]
That represents two vocabularies and a few terms from each vocabulary.
Here is the pipeline:
field_hwp_default_filter_values: - plugin: sub_process source: filters process: data: - plugin: array_template template: target_id: 'pipeline:' source: tids reference_field: - plugin: migration_lookup migration: hwp_vocabularies source: vid - plugin: array_template template: - field - 'pipeline:' - plugin: concat delimiter: _ - plugin: single_value - plugin: array_template template: - 'pipeline:' - data - reference_field - plugin: callback callable: array_column unpack_source: true - plugin: callback callable: serialize
After the first step in the pipeline (
sub_process
), the example at the top is converted to this:[ [ 'data' => [['target_id' => 1], ['target_id' => 2], ['target_id' => 3], ['target_id' => 5]], 'reference_field' => 'field_some_vocab', ], [ 'data' => [['target_id' => 8], ['target_id' => 13], ['target_id' => 21]], 'reference_field' => 'field_another_vocab', ], ]
By default, a process plugin (like
array_template
) is applied to each element of a source array. In this example,migration_lookup
is a no-op.The
single_value
process plugin overrides that default behavior, so the nextarray_template
prepares the input for thecallback
plugin:[ [['data' => ..., 'reference_field' => ...], ['data' => ..., 'reference_field' => ...]], 'data', 'reference_field', ]
and so the
callback
plugin returnsarray_column(..., 'data', 'reference_field')
or
[ 'field_some_vocab' => [['target_id' => 1], ['target_id' => 2], ['target_id' => 3], ['target_id' => 5]], 'field_another_vocab' => [['target_id' => 8], ['target_id' => 13], ['target_id' => 21]], ]
The last step in the pipeline uses
callback
withcallable: serialize
to serialize that array. - Status changed to Needs work
3 months ago 9:12pm 4 September 2024 - πΊπΈUnited States danflanagan8 St. Louis, US
I played around with this in Migrate Sandbox with great success. I also took my findings over to one of the related issues. ( β¨ Add wrapper process plugin to wrap/unwrap values in arrays Needs review )
I love being able to easily mix string literals with source properties and destination properties and (perhaps best of all) the pipeline value.
The test coverage is expansive, too. And the documentation isn't bad at all. Really nice stuff, @benjifisher.
My only complaint (well, I complained about something else way back in #7 that Benji humored me on) is that I feel weird referring about the trailing colon in
pipeline:
. It's a strange thing to type.At the same time, it's consistent with
source:
anddest:
, though with those you have to put something after the colon. IT's just that withpipeline:
you don't have to put anything after the colon and I would naively think that I would rarely put anything after the colon.And what would I suggest in place of that syntax that wouldn't simply be my personal preference just based on my personal tastes?
At the end of the day, I think I've convinced myself that the syntax on the MR is fine. Phew!
- Status changed to Needs review
3 months ago 4:53am 5 September 2024 - πΊπΈUnited States benjifisher Boston area
@danflanagan8:
I think I fixed all the things you pointed out on the MR.
I agree that
'pipeline:'
is awkward. I even considered allowingpipeline
as a synonym, but I think the Migrate API already goes too far in making things "convenient". If I did that, then I would have to add test coverage for it, too. In the end, I decided to keep the PHP simple and accept a little ugliness in the YAML for the sake of consistency. - Status changed to RTBC
3 months ago 8:26pm 9 September 2024 - πΊπΈUnited States danflanagan8 St. Louis, US
This is great stuff. Thanks, @benjifisher!
- First commit to issue fork.
-
heddn β
committed d2c5eba3 on 6.0.x authored by
benjifisher β
Issue #3440904 by benjifisher, danflanagan8, heddn: Process plugin:...
-
heddn β
committed d2c5eba3 on 6.0.x authored by
benjifisher β
- Status changed to Fixed
13 days ago 4:45pm 20 November 2024