You are here

public function TextFieldEmbedBase::getTargetEntities in Entity Usage 8.2

Same name and namespace in other branches
  1. 8.3 src/Plugin/EntityUsage/Track/TextFieldEmbedBase.php \Drupal\entity_usage\Plugin\EntityUsage\Track\TextFieldEmbedBase::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/TextFieldEmbedBase.php, line 17

Class

TextFieldEmbedBase
Base class for plugins tracking usage in entities embedded in WYSIWYG fields.

Namespace

Drupal\entity_usage\Plugin\EntityUsage\Track

Code

public function getTargetEntities(FieldItemInterface $item) {
  $item_value = $item
    ->getValue();
  if (empty($item_value['value'])) {
    return [];
  }
  $text = $item_value['value'];
  if ($item
    ->getFieldDefinition()
    ->getType() === 'text_with_summary') {
    $text .= $item_value['summary'];
  }
  $entities_in_text = $this
    ->parseEntitiesFromText($text);
  $valid_entities = [];
  foreach ($entities_in_text as $uuid => $entity_type) {

    // Check if the target entity exists since text fields are not
    // automatically updated when an entity is removed.
    if ($target_entity = $this->entityRepository
      ->loadEntityByUuid($entity_type, $uuid)) {
      $valid_entities[] = $target_entity
        ->getEntityTypeId() . "|" . $target_entity
        ->id();
    }
  }
  return array_unique($valid_entities);
}