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?

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Status: Needs review » Needs work

The last submitted patch, d8.typed-array-list-methods.patch, failed testing.

damiankloip’s picture

Status: Needs work » Needs review

Not sure how this would have an impact on that...

damiankloip’s picture

d8.typed-array-list-methods.patch queued for re-testing.

Status: Needs review » Needs work

The last submitted patch, d8.typed-array-list-methods.patch, failed testing.

damiankloip’s picture

Well, that's a different failure... :)

damiankloip’s picture

Status: Needs work » Needs review

d8.typed-array-list-methods.patch queued for re-testing.

Status: Needs review » Needs work

The last submitted patch, d8.typed-array-list-methods.patch, failed testing.

damiankloip’s picture

Different fail yes again, and the EntityTranslation fail again.

dawehner’s picture

Issue tags: +Needs reroll

Needs reroll. This method should be required in way more places now.

David Hernández’s picture

Issue tags: -Needs reroll +API addition

This looks like an API addition and I think it needs to be approved first.

David Hernández’s picture

Issue tags: +Needs reroll

Oops, removed the other tag...

pwolanin’s picture

Issue summary: View changes

should be tpArray() ?

alansaviolobo’s picture

I think the following classes also need to implement the methods once they are added to the interface.
DateTimeFIeldItemList
EntityReferenceFIeldItemList
FieldItemList
ItemList
Sequence

rpayanm’s picture

Status: Needs work » Needs review
FileSize
1.17 KB

Patch to review it :)

Status: Needs review » Needs work

The last submitted patch, 14: 1854782-14.patch, failed testing.

rpayanm’s picture

Status: Needs work » Needs review
FileSize
1.7 KB

fixing...

Status: Needs review » Needs work

The last submitted patch, 16: 1854782-16.patch, failed testing.

Status: Needs work » Needs review

rpayanm queued 16: 1854782-16.patch for re-testing.

Berdir’s picture

Title: Add get/setArray() methods to TypedData ListInterface » Add to/setArray() methods to TypedData ListInterface
Status: Needs review » Needs work
Issue tags: -Needs reroll

Yeah, sorry, should be toArray().

rpayanm’s picture

Status: Needs work » Needs review
FileSize
1.7 KB
1.32 KB
rpayanm’s picture

FileSize
1.7 KB

Ups... It's .patch :)

yched’s picture

Coming 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" ?

jhedstrom’s picture

+++ b/core/lib/Drupal/Core/Config/Schema/Sequence.php
@@ -89,4 +89,17 @@ public function set($index, $item) {
+    // @todo

Can this not be done now?

Berdir queued 21: 1854782-21.patch for re-testing.

Status: Needs review » Needs work

The last submitted patch, 21: 1854782-21.patch, failed testing.

tstoeckler’s picture

IMO instead of adding toArray() to ListInterface we should move it up from ComplexDataInterface to TraversableTypedDataInterface. With this patch we have identical methods on ComplexDataInterface and ListInterface, 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.

Jose Reyero’s picture

This 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...

tstoeckler’s picture

Knock 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().

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.