Problem/Motivation
Right now, it is impossible to set NULL as default value for at least the extract migrate process plugin.
If one needs NULL as default value at the extract process plugin, the only workaround I've found is setting it to '' (empty string)|0 (zero integer)|FALSE etc and add a 'default_value' process plugin without 'strict' (or with strict set to FALSE, with NULL default value.
So, this does not work:
[...]
process:
property:
plugin: extract
source: source_property
index:
- 0
- value
default_value: null
[...]
Workaround for the above:
[...]
process:
property:
-
plugin: extract
source: source_property
index:
- 0
- value
default_value: ''
-
plugin: default_value
default_value: null
[...]
Proposed resolution
From discussion on Slack the way to go is to use array_key_exists.
These are the plugins with a default_value or default configuration property and all but Extract allow NULL to be a default value. This issue has a patch for Extract.
- /migrate/src/Plugin/migrate/process/DefaultValue.php
- migrate/src/Plugin/migrate/process/Extract.php
- migrate/src/Plugin/migrate/process/FormatDate.php
- migrate/src/Plugin/migrate/process/NullCoalesce.php
- migrate/src/Plugin/migrate/process/StaticMap.php
Create a change record.
Remaining tasks
- Decide whether this is a bug, or a feature request. I'm unsure.
- Assess BC implications.
User interface changes
API changes
@tbd
Data model changes
Nothing.
Comments
Comment #2
huzookanull_coalesceanddefault_valueare already working as expected.Comment #3
huzookaComment #4
wim leersDiscovered in #3132101: Image media alt and title properties are not migrated to the corresponding image field properties.
Comment #5
quietone commentedSince there is a workaround I am not too keen on this. On the other hand it is a bit jarring to have the extract default: behave differently than the default_value plugin. So, I made a patch.
There are 23 migration yml files that use the extract plugin in core. A search of those did not find any that are followed by a default_value plugin for a destination property.
Removing the migrate-d7-d8 tag since this is a process plugin.
Comment #6
huzookaI think that this should be tested with
arrayandbooleantypes as well.s/translformed/transformed/
Comment #8
phenaproximaThere is a workaround, but it's undocumented and the existing behavior of
extractis confusing and unexpected anyway. I think NULL is a perfectly legitimate default value to expect fromextract, and from looking at the code, I suspect that the use of isset() was nothing more than an oversight.So, +1 for this generally. I'm not entirely sold on the approach of having
extractwrap around default_value as it does here, but I could be convinced. :)Comment #9
quietone commented@phenaproxima, I wrapped it around the default_value plugin to ensure that it always has the same behaviour. At the late hour when I did that it seemed the safer option than copying the code from default_value to extract.
Comment #10
mikelutzI'm not a fan either, Can we not just replace the isset check with a array_key_exists check and leave it at that?
Comment #11
quietone commentedSure can. It is now using an array_key_exists test. Since that changes the process plugin there is a new fail patch as well.
Comment #12
quietone commentedAh, I see I am back to using the patch extension on diffs.
Just reread the IS and it suggests that all process plugins that have a default property allow NULL to be set. The process plugins in core that have a default are:
1 core/modules/migrate/src/Plugin/migrate/process/DefaultValue.php:
2 core/modules/migrate/src/Plugin/migrate/process/Extract.php:
3
core/modules/migrate/src/Plugin/migrate/process/FormatDate.php:4 core/modules/migrate/src/Plugin/migrate/process/NullCoalesce.php:
5 core/modules/migrate/src/Plugin/migrate/process/StaticMap.php:
I think they all allow NULL to be the default value. Would be helpful is someone double checked that.
Edit: remove FormatDate
Comment #14
phenaproximaSo: in HEAD right now, the behavior is inconsistent across plugins. We should change Extract and NullCoalesce to use array_key_exists(). I think we should also be sure that each of these plugins has test coverage of NULL handling.
Comment #15
quietone commentedSorry, I meant to remove FormatDate.
Needs work NullCoalesce
Comment #16
quietone commentedNullCoalesce does not use array_key exists but it does return NULL if all the source values are NUL, and it even tells us that in the doc.
If someone does
The return value will be NULL, which is what is expected.
Not sure we should change the code but I have added a test that I think makes that behavior a little clearer.
Comment #17
mikelutzAgreed, Null Coalesce will return Null with no default value, and it will return it anyway if null is provided as default, though it wouldn't make sense to specify a default if you just wanted NULL to be the default.
RTBC from me on this one.
Comment #19
quietone commentedRandom test failure a few days ago and retested. Tests are passing so back to RTBC
Comment #20
alexpottCommitted and pushed ea0df4e831 to 9.1.x and 4768d66db0 to 9.0.x. Thanks!
I think this is a bug not a feature request - you set the default value in the extract plugin to NULL and it doesn't work. This fixes that.