You are here

abstract class TextFieldEmbedBase in Entity Usage 8.4

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

Hierarchy

Expanded class hierarchy of TextFieldEmbedBase

File

src/Plugin/EntityTrack/Track/TextFieldEmbedBase.php, line 12

Namespace

Drupal\entity_usage\Plugin\EntityTrack\Track
View source
abstract class TextFieldEmbedBase extends EntityUsageBase implements EmbedTrackInterface {

  /**
   * {@inheritdoc}
   */
  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);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EmbedTrackInterface::parseEntitiesFromText public function Parse an HTML snippet looking for embedded entities. 4
EntityUsageBase::$entityRepository protected property The EntityRepository service.
EntityUsageBase::$usageConfig protected property The usage config.
EntityUsageBase::$usageService protected property The usage tracking service.
EntityUsageBase::create public static function 2
EntityUsageBase::trackOnEntityCreation public function
EntityUsageBase::trackOnEntityDelete public function
EntityUsageBase::trackOnEntityUpdate public function
EntityUsageBase::__construct public function Plugin constructor. 2
TextFieldEmbedBase::getTargetEntities public function Retrieve the target entity(ies) from a field item value. Overrides EntityUsageBase::getTargetEntities