This is a spin off from discussion in #1846000: Add serializer support for JSON and AJAX.
Fago has suggested it would be good to add getArray and setArray methods to the Drupal\Core\TypedData\ListInterface, Otherwise we have to Iterate on the object whenever we want to get these.
This patch just adds these simple methods to the ListInterface and implements them in Drupal\Core\Entity\Field\Type\Field.
Do we need tests for this? There is nothing for lists in the TypedDataTests, unless they are somewhere else?
Comment | File | Size | Author |
---|---|---|---|
#21 | 1854782-21.patch | 1.7 KB | rpayanm |
#20 | 1854782-interdiff-19.txt | 1.32 KB | rpayanm |
#20 | 1854782-20.txt | 1.7 KB | rpayanm |
#16 | 1854782-16.patch | 1.7 KB | rpayanm |
#14 | 1854782-14.patch | 1.17 KB | rpayanm |
Comments
Comment #2
damiankloip CreditAttribution: damiankloip commentedNot sure how this would have an impact on that...
Comment #3
damiankloip CreditAttribution: damiankloip commentedd8.typed-array-list-methods.patch queued for re-testing.
Comment #5
damiankloip CreditAttribution: damiankloip commentedWell, that's a different failure... :)
Comment #6
damiankloip CreditAttribution: damiankloip commentedd8.typed-array-list-methods.patch queued for re-testing.
Comment #8
damiankloip CreditAttribution: damiankloip commentedDifferent fail yes again, and the EntityTranslation fail again.
Comment #9
dawehnerNeeds reroll. This method should be required in way more places now.
Comment #10
David Hernández CreditAttribution: David Hernández commentedThis looks like an API addition and I think it needs to be approved first.
Comment #11
David Hernández CreditAttribution: David Hernández commentedOops, removed the other tag...
Comment #12
pwolanin CreditAttribution: pwolanin commentedshould be tpArray() ?
Comment #13
alansaviolobo CreditAttribution: alansaviolobo commentedI think the following classes also need to implement the methods once they are added to the interface.
DateTimeFIeldItemList
EntityReferenceFIeldItemList
FieldItemList
ItemList
Sequence
Comment #14
rpayanmPatch to review it :)
Comment #16
rpayanmfixing...
Comment #19
BerdirYeah, sorry, should be toArray().
Comment #20
rpayanmComment #21
rpayanmUps... It's .patch :)
Comment #22
yched CreditAttribution: yched commentedComing from #2354485: Harmonize toArray() / getValue() on Entity / FieldItemList / FieldItem.
- setArray() looks fine as a use case, but it would better be named setItems() ?
(aligns with appendItem() & clearItems() added in #2164601: Stop auto-creating FieldItems on mere reading of $entity->field[N])
- in the interfaces where it currently exists, toArray() returns a fully-arrayified structure, safe for dsm(), and is thus what we advertise for easy debug output:
dsm($entity->toArray());
dsm($entity->field[0]->toArray());
That is not what the ItemList::toArray() method added here does : $entity->field->toArray(), being an array of FieldItem objects, would make dsm() die.
--> maybe getItems() for "return the Items as an array" ?
Comment #23
jhedstromCan this not be done now?
Comment #26
tstoecklerIMO instead of adding
toArray()
toListInterface
we should move it up fromComplexDataInterface
toTraversableTypedDataInterface
. With this patch we have identical methods onComplexDataInterface
andListInterface
, which is pointless and makes it harder for code that generically works with traversables.See also #2298687: Sequence and Mapping implement interfaces incorrectly, make them honest about what they support.
Comment #27
Jose Reyero CreditAttribution: Jose Reyero commentedThis is good and will simplify many other patches.
Fully agree with @yched #22 @tstoeckler #26
About the patch, instead of implementing it for sequence, we could just rename the existing getElements() to the new method (getItems?), we don't need to have both.
If no one else is working on this I may try a re-roll incorporating these latest comments...
Comment #28
tstoecklerKnock yourself out! :-)
Like I did in #2298687: Sequence and Mapping implement interfaces incorrectly, make them honest about what they support #22-2 should be easy with
iterator_to_array()
.