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.
Updated: Comment #N
Problem/Motivation
Right now, \Drupal\Core\TypedData\ListInterface only allows access to it's items through \ArrayAccess. That's nice where you can use and I think we should continue to support it, but it would be useful to have better methods instead of offsetGet(0), that's just weird.
Proposed resolution
Add first() and get($index). There's also a method for an toArray() (right now called getValue()), let's leave that out here.
Remaining tasks
Implement it.
User interface changes
None
API changes
Only additions.
Related Issues
Comment | File | Size | Author |
---|---|---|---|
#47 | list-methods-2110467-47.patch | 27.99 KB | Berdir |
#44 | list-methods-2110467-44-interdiff.txt | 455 bytes | Berdir |
#44 | list-methods-2110467-44.patch | 29.05 KB | Berdir |
#41 | list-methods-2110467-41-interdiff.txt | 761 bytes | Berdir |
#41 | list-methods-2110467-41.patch | 29.3 KB | Berdir |
Comments
Comment #1
BerdirThis adds get(), set() and first() and uses them where appropriate.
Also simplified some of the current usages, there's often no need to go through all those method calls and right now, magic is often the faster, due to internal optimizations.
Comment #3
BerdirUps.
Comment #5
BerdirInnteresting. So FieldItemListInterface already had a get() that forwarded to get() of the first item. That's kinda weird I don't think I've ever used that, let's see what happens if we remove it.
Comment #7
BerdirCan we add that gmail plugin that asks you math questions before letting you upload a patch at late hours to d.o, please? :)
Accidently added in an unrelated, weird change, must have been an accidental ctrl+v...
Comment #9
BerdirMore stupid mistakes.
Comment #11
BerdirThis gets embarassing. This actually passes all entity tests. Actually found two cases in EFQ that used the old get().
Comment #13
BerdirRe-roll and fixing tests.
Comment #14
Berdir13: list-methods-2110467-13.patch queued for re-testing.
Comment #15
yched CreditAttribution: yched commentedThis means we limit the "default reroute to the 1st item" sugar syntax strictly to $items->property_name ?
Why not, just pointing that out.
Also, this code here could then just be $entity->{$field_name}->entity ?
Same here ?
(and possibly in other places, didn't check all)
Comment #16
Berdir1. Yes, that's what currently happens in this patch, which was also the main problem @fago had with the patch. I actually never expected get() to work like this, was suprised to see that. We could either add a special method for that use case on FieldItemList or we could rename get() on ListInterface() to something else. But I don't have any good names for either of those options. Thoughts?
Comment #17
yched CreditAttribution: yched commentedI think I personally could live with ItemList->get() being just about retrieving a list item by its key, and have the "implicit route to delta 0" DX sugar restricted to just $list->property.
But I'm not sure what the impact would be on the TypedData side ? I'm thinking of "generic code" like validation or normalization, that uses the $item as a TypedData and maybe relies on the current get() behavior ?
Comment #18
BerdirTypedData/ComplexData never had that get() shortcut, that only existed in FieldItemListInterface.
And for generic code, the new behavior IMHO makes more sense, because you don't want to special case the single value case anyway, you should foreach over each level taht is complexData or ListInterface.
Comment #19
yched CreditAttribution: yched commentedOh - then yes, I'm +1 on that.
This is still for @fago to sign off, though. Assigning to him.
Comment #20
Berdir13: list-methods-2110467-13.patch queued for re-testing.
Comment #22
BerdirRe-roll, still needs @fago's sign off.
Comment #24
BerdirFixing those new test fails. The code incorrectly assumed that it's working with a field_item not a list, as visible in the variable name.
Comment #25
fagoGeneric code would have no knowledge of get() on a list. Anyway I like the consistency brought to get method. If the little extra-magic within the magic doesn't bother you, it should be fine to others also. I'm not feeling strong about it either, so let's do it so then!
Misses the return doc of returning a typed data object. Also, what does it return if the item does not exist?
For field items we chose to always instantiate empty items, thus I guess we should document it that way here. If it's not set, then the returned object is empty (or contains NULL).
Usually we do setters which return "static", so we can chain through. I'd suggest doing that here as well.
Misses the interface, it's always a typed data object.
Comment #26
BerdirOk, fixed that.
Comment #27
fagoaccording to amateescu's experiences that should be static
violates the interface as it misses the return.
Comment #28
BerdirOk, better now? :)
Comment #29
Berdir28: list-methods-2110467-28.patch queued for re-testing.
Comment #30
fagoYep, thanks. Setting RTBC, assuming bot stays green.
Comment #31
xjm28: list-methods-2110467-28.patch queued for re-testing.
Comment #33
BerdirSequence introduced a get() method and I have no idea what it does, any pointers?
Comment #34
fagoSequences seem to be the same as lists. I've no idea how the get() is intended to be used though, but given that it's just a list I'd assume it should map to the proposed meaning already.
Comment #35
BerdirExcept it doesn't and seems to be doing something special by calling $this->parse() ?
Comment #36
fagoall the typed config objects do that parsing, e.g. see the Mapping class. Not sure why it is required, but it seems to do nothing else than creating contained typed-data objects but just via the typed-config factory.
Comment #37
BerdirAh, you are right, I got confused by the slightly different (and better) implementation in offsetGet(), but it is essentially the same.
Here's a re-roll.
Comment #38
yched CreditAttribution: yched commentedonly semi-related, but shameless plug : #2164601: Stop auto-creating FieldItems on mere reading of $entity->field[N]
Comment #39
BerdirCould use a quick check from someone before setting back to RTBC as this wasn't just a re-roll, I merged in the new get() method on Sequence, no relevant changes in other files, though.
Comment #40
fagoIs adding this here intentional?
Else, the patch looks good to me.
Comment #41
BerdirUh, no, not sure how that happened.
Comment #42
fagoComment #43
alexpottNeeds a reroll
Plus...
Unused use.
Comment #44
BerdirComment #45
BerdirRe-roll was trivial this time, so back to RTBC unless testbot for some strange reason disagrees.
Comment #46
alexpottHopefully the reroll is trivial again...
Comment #47
BerdirYeah, LegacyFieldItemList doesn't exist anymore :)
Comment #48
BerdirThe patches are identical apart from changes in the patch context and the removed file.
Comment #49
alexpottCommitted 19e8cf3 and pushed to 8.x. Thanks!