public EntityReferenceFieldItemList::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 EntityReferenceFieldItemListInterface::referencedEntities
File
- core/lib/Drupal/Core/Field/EntityReferenceFieldItemList.php, line 26
Class
- EntityReferenceFieldItemList
- Defines a item list class for entity reference fields.
Namespace
Drupal\Core\Field
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | public function referencedEntities() { if ( empty ( $this ->list)) { return array (); } // Collect the IDs of existing entities to load, and directly grab the // "autocreate" entities that are already populated in $item->entity. $target_entities = $ids = array (); 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::entityManager()->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 ; } |
Please login to continue.