\Drupal\Core\Field\FieldItemListInterface $field:
array $field_item:
$delta:
$property:
bool $is_target_type_translatable: (optional) Whether the target entity type is translatable.
\Drupal\Core\Entity\ContentEntityInterface|null
protected function findReferencedEntity(FieldItemListInterface $field, array $field_item, $delta, $property, $is_target_type_translatable = TRUE) {
// If an id is provided, loop over the field item deltas until we find the
// matching entity. In case of untranslatable target types return the
// source target entity as it will be duplicated.
if (isset($field_item[$property]['#id'])) {
foreach ($field as $item_delta => $item) {
if ($item->{$property} instanceof ContentEntityInterface) {
/** @var ContentEntityInterface $referenced_entity */
$referenced_entity = $item->{$property};
if ($referenced_entity
->id() == $field_item[$property]['#id'] || $item_delta === $delta && !$is_target_type_translatable) {
return $referenced_entity;
}
}
}
// @todo Support loading an entity, throw an exception or log a warning?
}
elseif ($field
->offsetExists($delta) && $field
->offsetGet($delta)->{$property} instanceof ContentEntityInterface) {
return $field
->offsetGet($delta)->{$property};
}
}