Clarify/unify the downcast logic in ContentEntityBase::__set() / ItemList::set() / FieldItemBase::__set()

Created on 21 February 2015, almost 10 years ago
Updated 30 January 2023, almost 2 years ago

Spin off from #2426509: ContentEntityBase::__set() messes with values that happen to be TypedData

ContentEntityBase::__set(), ItemList::set() and FieldItemBase::__set() all have special logic to downcast assigned values that happen to be a TypedDataInterface to their ->getValue() array before assigning it.

The exact intent of that at each level is not fully clear.

- In ContentEntityBase::__set(), this allows easy copying of field values :
$e1->field = $e2->field; // does: $e1->field->setValue($e2->field->getValue())
(we copy the values, but don't actually reuse the same FieldItemList object)

--> @fago suggested we limit that behavior to FieldItemListInterface instead of "anything that is a TypedData".
Anything else gets passed as is to ItemList->setValue(), and if not an array, gets assigned to the first property of the first item.

- In ItemList::offsetSet()/set(), this allows easy copying of item values:
$e1->field[0] = $e2->field[1]; // does: $e1->field[0]->setValue($e2->field[1]->getValue())

--> Similarly, do we want FieldItemList to override that and limit that behavior to FieldItemInterface ?

- In FieldItemBase::__set(), well, it's not clear to me what this one is for, nor what it allows or forbids :-)

--> Do we want to keep it ?
--> If we do, then by the same reasoning than #2426509: ContentEntityBase::__set() messes with values that happen to be TypedData , it seems that behavior should only be applied to the "official properties" in the FieldItem, not to arbitrary runtime properties
--> What is done there is "downcast if TypedDataInterface **and not EntityInterface**". That part about EntityInterface is moot now, since Entities ar not TypedData anymore.

📌 Task
Status

Needs work

Version

10.1

Component
Field 

Last updated 5 days ago

Created by

🇫🇷France yched

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • The Needs Review Queue Bot tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

    Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.

    Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

Production build 0.71.5 2024