You are here

public function EntityReference::getTargetEntities in Entity Usage 8.3

Same name and namespace in other branches
  1. 8.2 src/Plugin/EntityUsage/Track/EntityReference.php \Drupal\entity_usage\Plugin\EntityUsage\Track\EntityReference::getTargetEntities()

Retrieve the target entity(ies) from a field item value.

Parameters

\Drupal\Core\Field\FieldItemInterface $item: The field item to get the target entity(ies) from.

Return value

string[] An indexed array of strings where each target entity type and ID are concatenated with a "|" character. Will return an empty array if no target entity could be retrieved from the received field item value.

Overrides EntityUsageTrackInterface::getTargetEntities

File

src/Plugin/EntityUsage/Track/EntityReference.php, line 23

Class

EntityReference
Tracks usage of entities related in entity_reference fields.

Namespace

Drupal\entity_usage\Plugin\EntityUsage\Track

Code

public function getTargetEntities(FieldItemInterface $item) {

  /** @var \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem $item */
  $item_value = $item
    ->getValue();
  if (empty($item_value['target_id'])) {
    return [];
  }
  $target_type = $item
    ->getFieldDefinition()
    ->getSetting('target_type');

  // Only return a valid result if the target entity exists.
  if (!$this->entityTypeManager
    ->getStorage($target_type)
    ->load($item_value['target_id'])) {
    return [];
  }
  $target_type_and_id = $target_type . '|' . $item_value['target_id'];

  // For paragraph references, send back the revision ID as well, since this
  // will allow the system to load that revision and check its fields.
  if (!empty($item_value['target_revision_id'])) {
    $target_type_and_id = $target_type_and_id . '|' . $item_value['target_revision_id'];
  }
  return [
    $target_type_and_id,
  ];
}