You are here

class EmbeddedImagesCollector in Dependency Calculation 8

Class EmbeddedImagesCollector.

@package Drupal\depcalc\EventSubscriber\DependencyCollector

Hierarchy

  • class \Drupal\depcalc\EventSubscriber\DependencyCollector\BaseDependencyCollector implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of EmbeddedImagesCollector

1 file declares its use of EmbeddedImagesCollector
EmbeddedImagesCollectorTest.php in tests/src/Kernel/EventSubscriber/DependencyCollector/EmbeddedImagesCollectorTest.php
1 string reference to 'EmbeddedImagesCollector'
depcalc.services.yml in ./depcalc.services.yml
depcalc.services.yml
1 service uses EmbeddedImagesCollector
embedded_images.calculator in ./depcalc.services.yml
Drupal\depcalc\EventSubscriber\DependencyCollector\EmbeddedImagesCollector

File

src/EventSubscriber/DependencyCollector/EmbeddedImagesCollector.php, line 20

Namespace

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

  /**
   * Database connection.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $database;

  /**
   * The module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * EmbeddedImagesCollector constructor.
   *
   * @param \Drupal\Core\Database\Connection $database
   *   Database connection.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   Module handler.
   */
  public function __construct(Connection $database, ModuleHandlerInterface $module_handler) {
    $this->database = $database;
    $this->moduleHandler = $module_handler;
  }

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

  /**
   * Reacts on CALCULATE_DEPENDENCIES event.
   *
   * @param \Drupal\depcalc\Event\CalculateEntityDependenciesEvent $event
   *   Event.
   *
   * @throws \Exception
   */
  public function onCalculateDependencies(CalculateEntityDependenciesEvent $event) {
    if (!$this->moduleHandler
      ->moduleExists('file')) {
      return;
    }
    $entity = $event
      ->getEntity();
    if (FALSE === $entity instanceof ContentEntityInterface) {
      return;
    }
    $files = $this
      ->getAttachedFiles($entity, 'editor');
    foreach ($files as $file) {
      $this
        ->addDependency($event, $file);
    }
  }

  /**
   * Builds list of attached files.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   Entity.
   * @param string $module
   *   Module name.
   *
   * @return \Drupal\file\Entity\File[]
   *   Files list.
   */
  protected function getAttachedFiles(EntityInterface $entity, string $module = 'file') : array {
    $criteria = new Condition('AND');
    $criteria
      ->condition('type', $entity
      ->getEntityTypeId())
      ->condition('count', '0', '>')
      ->condition('module', [
      $module,
    ], 'in')
      ->condition('id', $entity
      ->id());
    $rows = $this->database
      ->select('file_usage', 'f')
      ->fields('f', [
      'fid',
    ])
      ->condition($criteria)
      ->execute()
      ->fetchAllAssoc('fid');
    if (empty($rows)) {
      return [];
    }
    $fids = array_keys($rows);
    return File::loadMultiple($fids);
  }

  /**
   * Add dependency.
   *
   * @param \Drupal\depcalc\Event\CalculateEntityDependenciesEvent $event
   *   Event.
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   Entity.
   *
   * @throws \Exception
   */
  protected function addDependency(CalculateEntityDependenciesEvent $event, EntityInterface $entity) : void {
    if ($event
      ->getStack()
      ->hasDependency($entity
      ->uuid())) {
      return;
    }
    $entity_wrapper = new DependentEntityWrapper($entity);
    $local_dependencies = [];
    $entity_wrapper_dependencies = $this
      ->getCalculator()
      ->calculateDependencies($entity_wrapper, $event
      ->getStack(), $local_dependencies);
    $this
      ->mergeDependencies($entity_wrapper, $event
      ->getStack(), $entity_wrapper_dependencies);
    $event
      ->addDependency($entity_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.
EmbeddedImagesCollector::$database protected property Database connection.
EmbeddedImagesCollector::$moduleHandler protected property The module handler.
EmbeddedImagesCollector::addDependency protected function Add dependency.
EmbeddedImagesCollector::getAttachedFiles protected function Builds list of attached files.
EmbeddedImagesCollector::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
EmbeddedImagesCollector::onCalculateDependencies public function Reacts on CALCULATE_DEPENDENCIES event.
EmbeddedImagesCollector::__construct public function EmbeddedImagesCollector constructor.