You are here

class EntityViewDisplayDependencyCollector in Dependency Calculation 8

Subscribes to dependency collection to extract the entity view display.

Hierarchy

Expanded class hierarchy of EntityViewDisplayDependencyCollector

1 string reference to 'EntityViewDisplayDependencyCollector'
depcalc.services.yml in ./depcalc.services.yml
depcalc.services.yml
1 service uses EntityViewDisplayDependencyCollector
entity_view_display.dependency_calculator in ./depcalc.services.yml
Drupal\depcalc\EventSubscriber\DependencyCollector\EntityViewDisplayDependencyCollector

File

src/EventSubscriber/DependencyCollector/EntityViewDisplayDependencyCollector.php, line 14

Namespace

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

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * EntityViewDisplayDependencyCollector constructor.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager) {
    $this->entityTypeManager = $entity_type_manager;
  }

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

  /**
   * Calculates the associated entity view display.
   *
   * @param \Drupal\depcalc\Event\CalculateEntityDependenciesEvent $event
   *   The dependency calculation event.
   */
  public function onCalculateDependencies(CalculateEntityDependenciesEvent $event) {
    if ($event
      ->getEntity() instanceof ContentEntityInterface) {
      $storage = $this->entityTypeManager
        ->getStorage('entity_view_display');
      $entity = $event
        ->getEntity();
      $ids = $this->entityTypeManager
        ->getStorage('entity_view_display')
        ->getQuery('AND')
        ->condition('status', TRUE)
        ->condition('targetEntityType', $entity
        ->getEntityTypeId())
        ->condition('bundle', $entity
        ->bundle())
        ->execute();
      if ($ids) {
        $displays = $storage
          ->loadMultiple($ids);
        foreach ($displays as $display) {
          $display_wrapper = new DependentEntityWrapper($display);
          $local_dependencies = [];
          $this
            ->mergeDependencies($display_wrapper, $event
            ->getStack(), $this
            ->getCalculator()
            ->calculateDependencies($display_wrapper, $event
            ->getStack(), $local_dependencies));
          $event
            ->addDependency($display_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.
EntityViewDisplayDependencyCollector::$entityTypeManager protected property The entity type manager.
EntityViewDisplayDependencyCollector::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
EntityViewDisplayDependencyCollector::onCalculateDependencies public function Calculates the associated entity view display.
EntityViewDisplayDependencyCollector::__construct public function EntityViewDisplayDependencyCollector constructor.