referencedEntities() returns empty array for computed fields

Created on 27 February 2024, 10 months ago
Updated 5 August 2024, 5 months ago
$node = \Drupal::entityTypeManager()->getStorage('node')->load(123);
$referenced_entities = $node->get('my_computed_err_field')->referencedEntities();
// $referenced_entities is always empty

I have a computed EER field on one of my content types and noticed since updating to 8.x-1.11 that referencedEntities() no longer returns any entities.

Looks like this bug was introduced in 🐛 referencedEntities() causes data loss Needs review .

The current referencedEntities() implementation iterates $this->list, but for computed fields this may not be computed yet.

  /**
   * {@inheritdoc}
   */
  public function referencedEntities() {
    $target_entities = [];
    foreach ($this->list as $delta => $item) {
      if ($item->entity) {
        $target_entities[$delta] = $item->entity;
      }
    }
    return $target_entities;
  }

I'd like to suggest to do the same as Drupal core's implementation of referencedEntities(), e.g. add an isEmpty() check at the beginning of the method. This makes sure is the value has been computed.

🐛 Bug report
Status

Needs review

Version

1.0

Component

Code

Created by

🇧🇪Belgium rp7

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

Merge Requests

Comments & Activities

Production build 0.71.5 2024