Problem/Motivation
FieldItemList::filterEmptyItems() removes Items and renumbers deltas.
But each Item is supposed to know its own delta (its "name" - i.e the way my parent calls me), and we do not update that.
// Put two values, first one being empty.
$node->field_text = array('', 'bar');
$node->field_text->count(); // -> 2
$node->field_text[1]->value; // -> 'bar'
$node->field_text[1]->getName(); // -> 1
// Filter the first value.
$node->field_text->filterEmptyValue();
$node->field_text->count(); // -> 1
$node->field_text[0]->value; // -> 'bar'
$node->field_text[0]->getName(); // -> oops, still 1...
Proposed resolution
Patch adds a generic ItemList::filter($callback) method, which takes care of reassigning the correct delta in each Item.
FieldItemList::filterEmptyItems() now relies on that.
Comments
Comment #1
yched commentedPatch adds a generic ItemList::filter($callback) method, which takes care of reassigning the correct delta in each Item.
FieldItemList::filterEmptyItems() now relies on that.
Working on a test, but setting to NR for bot & feedback.
Comment #3
yched commentedSilly last minute incomplete var rename.
Comment #4
yched commentedWith test.
Interdiff = the test-only patch.
Comment #6
amateescu commented$this, not this :P
Otherwise the patch looks good to me but we should probably ask @fago or @Berdir to take a look as well.
Comment #7
yched commentedHeh, can't seem to get those right :-p
Comment #8
yched commentedComment #9
fagoI think this is a good API addition, but as it's a separate method now I think it needs some dedicated test coverage in the typed tests as well.
Also, this extends ListInterface - which should be fine as I do not expect anyone to implement it on its own right now. Still, not sure whether this should be mentioned in some change record?
Comment #10
fagoHaving an $item->getName() lying to the caller seems to be rather fundamentally bad thing to me, thus bumping to major.
Comment #11
yched commentedReroll for now.
Comment #12
yched commentedTests added.
Comment #13
yched commentedDraft change notice added at https://www.drupal.org/node/2380287
RTBC anyone ? :-)
Comment #14
yched commentedBump - @fago's remarks have been adressed, this just waiting for an RTBC.
Comment #15
amateescu commentedLet me help you with that :)
Comment #16
fagoThanks, added test coverage looks good. The change record draft reads good as well, I improved its formatting a bit though.
Not sure how that sneaked in, but that's a coding style regression? Thus, setting back to NW for that.
Comment #17
yched commentedOops, indeed.
Just a code style fix, so back to RTBC.
Comment #18
yched commented@fago: thanks for the adjustements on the change record. I edited it to re-add the notion of "subsequent keys starting at 0".
Comment #19
alexpottThis issue addresses a major bug and is allowed per https://www.drupal.org/core/beta-changes. Committed e55a223 and pushed to 8.0.x. Thanks!
Comment #21
fago@yched: Thanks, agreed - it makes sense to clarify that.
@alex: Thanks, I published the change record then.