ContentEntityBase::toArray should return the whole entity structure by including computed properties

Created on 28 November 2016, about 8 years ago
Updated 28 March 2024, 9 months ago

Problem/Motivation

ContentEntityBase::toArray will currently only return the field values, but when working with nested entity forms and a lot of references it would be really useful if toArray would return the referenced entities as well.

Where I've noticed the need for this is in the following use case:
We deal a lot with entity references and in hook_entity_presave for new entities we create, save and attach to a specific field other entities.
This looks e.g. like this
$node = $storage->create();
$node->save();

What has happen now is that we have created and saved a node entity with couple dynamically created and saved referenced entities. However none of the entities here is in the static entity cache.

Now the code follows:
$node->addTranslation('en', $node->toArray());

And here we create a translation but when we try accessing the referenced entities they will be loaded newly from the storage instead being the same objects already loaded on the previous node translation. Changing ::toArray to include the computed properties would solve this issue.

Proposed resolution

Add an $include_computed parameter to ContentEntityBase::toArray.

Remaining tasks

User interface changes

API changes

Data model changes

Feature request
Status

Needs work

Version

11.0 🔥

Component
Entity 

Last updated about 16 hours ago

Created by

🇩🇪Germany hchonov 🇪🇺🇩🇪🇧🇬

Live updates comments and jobs are added and updated live.
  • Needs change record

    A change record needs to be drafted before an issue is committed. Note: Change records used to be called change notifications.

  • Needs issue summary update

    Issue summaries save everyone time if they are kept up-to-date. See Update issue summary task instructions.

Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

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

Production build 0.71.5 2024