public function DynamicEntityReferenceFieldItemList::referencedEntities in Dynamic Entity Reference 8.2
Same name and namespace in other branches
- 8 src/Plugin/Field/FieldType/DynamicEntityReferenceFieldItemList.php \Drupal\dynamic_entity_reference\Plugin\Field\FieldType\DynamicEntityReferenceFieldItemList::referencedEntities()
Gets the entities referenced by this field, preserving field item deltas.
Return value
\Drupal\Core\Entity\EntityInterface[] An array of entity objects keyed by field item deltas.
Overrides EntityReferenceFieldItemList::referencedEntities
File
- src/
Plugin/ Field/ FieldType/ DynamicEntityReferenceFieldItemList.php, line 40
Class
- DynamicEntityReferenceFieldItemList
- Defines a item list class for dynamic entity reference fields.
Namespace
Drupal\dynamic_entity_reference\Plugin\Field\FieldTypeCode
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 && $item->target_type !== NULL) {
$ids[$item->target_type][$delta] = $item->target_id;
}
elseif ($item
->hasNewEntity()) {
$target_entities[$delta] = $item->entity;
}
}
// Load and add the existing entities.
if ($ids) {
foreach ($ids as $target_type => $entity_type_ids) {
$entities = \Drupal::entityTypeManager()
->getStorage($target_type)
->loadMultiple($entity_type_ids);
foreach ($entity_type_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;
}