function EntityReferenceFieldItemList::referencedEntities

Same name in other branches
  1. 8.9.x core/lib/Drupal/Core/Field/EntityReferenceFieldItemList.php \Drupal\Core\Field\EntityReferenceFieldItemList::referencedEntities()
  2. 10 core/lib/Drupal/Core/Field/EntityReferenceFieldItemList.php \Drupal\Core\Field\EntityReferenceFieldItemList::referencedEntities()
  3. 11.x core/lib/Drupal/Core/Field/EntityReferenceFieldItemList.php \Drupal\Core\Field\EntityReferenceFieldItemList::referencedEntities()

Overrides EntityReferenceFieldItemListInterface::referencedEntities

3 calls to EntityReferenceFieldItemList::referencedEntities()
FileFieldItemList::delete in core/modules/file/src/Plugin/Field/FieldType/FileFieldItemList.php
Defines custom delete behavior for field values.
FileFieldItemList::deleteRevision in core/modules/file/src/Plugin/Field/FieldType/FileFieldItemList.php
Defines custom revision delete behavior for field values.
FileFieldItemList::postSave in core/modules/file/src/Plugin/Field/FieldType/FileFieldItemList.php
Defines custom post-save behavior for field values.

File

core/lib/Drupal/Core/Field/EntityReferenceFieldItemList.php, line 26

Class

EntityReferenceFieldItemList
Defines an item list class for entity reference fields.

Namespace

Drupal\Core\Field

Code

public function referencedEntities() {
    if ($this->isEmpty()) {
        return [];
    }
    // Collect the IDs of existing entities to load, and directly grab the
    // "autocreate" entities that are already populated in $item->entity.
    $target_entities = $ids = [];
    foreach ($this->list as $delta => $item) {
        if ($item->target_id !== NULL) {
            $ids[$delta] = $item->target_id;
        }
        elseif ($item->hasNewEntity()) {
            $target_entities[$delta] = $item->entity;
        }
    }
    // Load and add the existing entities.
    if ($ids) {
        $target_type = $this->getFieldDefinition()
            ->getSetting('target_type');
        $entities = \Drupal::entityTypeManager()->getStorage($target_type)
            ->loadMultiple($ids);
        foreach ($ids as $delta => $target_id) {
            if (isset($entities[$target_id])) {
                $target_entities[$delta] = $entities[$target_id];
            }
        }
        // Ensure the returned array is ordered by deltas.
        ksort($target_entities);
    }
    return $target_entities;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.