You are here

class MediaEmbed in Entity Usage 8.4

Tracks usage of drupal-media tags in wysiwyg fields.

Plugin annotation


@EntityTrack(
  id = "media_embed",
  label = @Translation("Media WYSIWYG Embed (Core)"),
  description = @Translation("Tracks relationships created with Core's 'Embed media' filter in formatted text fields."),
  field_types = {"text", "text_long", "text_with_summary"},
)

Hierarchy

Expanded class hierarchy of MediaEmbed

File

src/Plugin/EntityTrack/Track/MediaEmbed.php, line 17

Namespace

Drupal\entity_usage\Plugin\EntityTrack\Track
View source
class MediaEmbed extends TextFieldEmbedBase {

  /**
   * {@inheritdoc}
   */
  public function parseEntitiesFromText($text) {
    $dom = Html::load($text);
    $xpath = new \DOMXPath($dom);
    $entities = [];
    foreach ($xpath
      ->query('//drupal-media[@data-entity-type="media" and @data-entity-uuid]') as $node) {

      // Note that this does not cover 100% of the situations. In the (unlikely
      // but possible) use case where the user embeds the same entity twice in
      // the same field, we are just recording 1 usage for this target entity,
      // when we should record 2. The alternative is to add a lot of complexity
      // to the update logic of our plugin, to deal with all possible
      // combinations in the update scenario.
      // @TODO Re-evaluate if this is worth the effort and overhead.
      $entities[$node
        ->getAttribute('data-entity-uuid')] = $node
        ->getAttribute('data-entity-type');
    }
    return $entities;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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
MediaEmbed::parseEntitiesFromText public function Parse an HTML snippet looking for embedded entities. Overrides EmbedTrackInterface::parseEntitiesFromText
TextFieldEmbedBase::getTargetEntities public function Retrieve the target entity(ies) from a field item value. Overrides EntityUsageBase::getTargetEntities