Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
In my migration process I have something like this:
sortiment:
plugin: static_map
source: 'Sortiment 1'
default_value: null
map:
order: 1
collect: 2
I want this to set my field to N/A if no value is set in the "Sortiment 1" source field.
What happens is it skips the row.
I have located this in core/modules/migrate/src/Plugin/migrate/process/StaticMap.php, ln 43
if (isset($this->configuration['default_value'])) {
isset() will return FALSE if testing a variable that has been set to NULL.
Is this intended behavior (to have a hidden skip row functionality (trap)? I'm happy to provide a patch ortherwise.
Comment | File | Size | Author |
---|---|---|---|
#21 | interdiff.txt | 763 bytes | mikeryan |
#21 | default_value_null_in-2613178-21.patch | 1.77 KB | mikeryan |
#9 | default_value_null_in-2613178-9.patch | 1.82 KB | mikeryan |
#9 | default_value_null_in-2613178-9-FAIL.patch | 997 bytes | mikeryan |
#3 | default_value_null_in-2613178-3.patch | 818 bytes | mikeryan |
Comments
Comment #2
mikeryanJust ran into this myself, still there in 8.1.x.
Comment #3
mikeryanSeems simple enough.
Comment #4
mikeryanComment #5
heddnIt this a duplicate of #2692373: Value should be null when is produced skip process? That one has tests.
Comment #6
mikeryanNope, this one is purely a static_map issue, which that issue doesn't touch.
Comment #7
heddnIt is best to do this with both isset() || array_key_exists(). See http://www.zomeoff.com/php-fast-way-to-determine-a-key-elements-existanc...
Comment #8
heddnComment #9
mikeryanNow with testy goodness!
Comment #10
heddnAssuming this comes back green, this is RTBC.
Comment #13
mikeryanThe failures don't appear to have anything to do with this change, requeueing.
Comment #14
quietone CreditAttribution: quietone as a volunteer commented@heddn, thanks for the link in #7. And to mikeryan for doing all the work.
Comment #15
heddnI never did see a clean failure. But assuming that the reque-ed test fails cleanly, +1 on RTBC.
Comment #16
alexpottI think this should just be
https://www.drupal.org/files/issues/default_value_null_in-2613178-9.patch
the OR is unnecessary.Comment #17
mikeryanA mispaste? You mean we should just do the array_key_exists()?
Thanks.
Comment #18
heddnHmm, isset is always a little tough in php. But me thinks that it is possible to provide a static value of NULL. I've actually done that in at least one case when I wanted to null out the destination of an entity reference. I think I need more description on why #9 seems incomplete.
Comment #19
alexpott@mikeryan yeah
is the same as
Comment #20
alexpott@heddn in every case that
isset()
returns TRUEarray_key_exists()
will also return TRUE. Howeverarray_key_exists()
will also return TRUE if the value is NULL. Which is what we want here. By removing the||
we make it simpler for a reviewer or ourselves in 6 months time to look at this code and work out what we were thinking.Comment #21
mikeryanOK, back to the original version.
Comment #22
heddn@alexpott, the both together is a micro-optimization I use all the time. But it probably won't increase speed all *that* much. No blocker from me. Back to RTBC.
Comment #23
alexpottYeah it is a super super micro-optimisation which shouldn't matter at all in a migration - there is going to be much slower things going on. Also in PHP7 the optimisation completely disappears. See https://ilia.ws/archives/247-Performance-Analysis-of-isset-vs-array_key_... - I've run that script on PHP7 and there's no difference between isset and array_key_exists.
@mikeryan - sorry I didn't notice the earlier discussion. I would have just committed the first patch.
Committed 4149ee4 and pushed to 8.1.x and 8.2.x. Thanks!