You are here

class DrupalMediaEmbedCollector in Dependency Calculation 8

Class DrupalMediaEmbedCollector.

Hierarchy

Expanded class hierarchy of DrupalMediaEmbedCollector

1 string reference to 'DrupalMediaEmbedCollector'
depcalc.services.yml in ./depcalc.services.yml
depcalc.services.yml
1 service uses DrupalMediaEmbedCollector
drupal_media_embed.embed.calculator in ./depcalc.services.yml
Drupal\depcalc\EventSubscriber\DependencyCollector\DrupalMediaEmbedCollector

File

src/EventSubscriber/DependencyCollector/DrupalMediaEmbedCollector.php, line 17

Namespace

Drupal\depcalc\EventSubscriber\DependencyCollector
View source
class DrupalMediaEmbedCollector extends BaseDependencyCollector {

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[DependencyCalculatorEvents::CALCULATE_DEPENDENCIES][] = [
      'onCalculateDependencies',
    ];
    return $events;
  }

  /**
   * Calculates media entities embedded into the text areas of other entities.
   *
   * @param \Drupal\depcalc\Event\CalculateEntityDependenciesEvent $event
   *   The CalculateEntityDependenciesEvent event.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  public function onCalculateDependencies(CalculateEntityDependenciesEvent $event) {
    $entity = $event
      ->getEntity();
    if (!$entity instanceof ContentEntityInterface) {
      return;
    }
    $fields = FieldExtractor::getFieldsFromEntity($entity, function (ContentEntityInterface $entity, $field_name, FieldItemListInterface $field) {
      $storage_definition = $field
        ->getFieldDefinition()
        ->getFieldStorageDefinition();
      return $storage_definition instanceof FieldStorageConfig && $storage_definition
        ->getTypeProvider() === 'text';
    });
    foreach ($fields as $field) {
      foreach ($field
        ->getValue() as $value) {
        if (empty($value['format'])) {
          continue;
        }

        /** @var \Drupal\filter\Entity\FilterFormat $filter_format */
        $filter_format = \Drupal::entityTypeManager()
          ->getStorage('filter_format')
          ->load($value['format']);
        $filters = $filter_format
          ->filters();
        $filters
          ->sort();

        /** @var \Drupal\filter\Plugin\FilterInterface $filter */
        foreach ($filters as $filter) {

          // If this text area can have entities embedded, we want to
          // manually extract the entities contained therein.
          $text = $value['value'];
          if ($filter
            ->getPluginId() !== 'media_embed' || stristr($text, '<drupal-media') === FALSE) {
            continue;
          }
          $dom = Html::load($text);
          $xpath = new \DOMXPath($dom);
          foreach ($xpath
            ->query('//drupal-media[@data-entity-type="media" and normalize-space(@data-entity-uuid)!=""]') as $node) {

            /** @var \DOMElement $node */
            $entity_type = $node
              ->getAttribute('data-entity-type');
            if ($uuid = $node
              ->getAttribute('data-entity-uuid')) {
              $embed = \Drupal::entityTypeManager()
                ->getStorage($entity_type)
                ->loadByProperties([
                'uuid' => $uuid,
              ]);
              if (!is_array($embed)) {
                continue;
              }
              $embed = current($embed);
              $embed_wrapper = new DependentEntityWrapper($embed);
              $local_dependencies = [];
              $this
                ->mergeDependencies($embed_wrapper, $event
                ->getStack(), $this
                ->getCalculator()
                ->calculateDependencies($embed_wrapper, $event
                ->getStack(), $local_dependencies));
              $event
                ->addDependency($embed_wrapper);
            }
          }
        }
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BaseDependencyCollector::getCalculator protected function Gets the dependency calculator.
BaseDependencyCollector::mergeDependencies protected function Properly adds dependencies and their modules to a wrapper object.
DrupalMediaEmbedCollector::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
DrupalMediaEmbedCollector::onCalculateDependencies public function Calculates media entities embedded into the text areas of other entities.