You are here

public function PathAliasEntityCollector::onCalculateDependencies in Dependency Calculation 8

File

src/EventSubscriber/DependencyCollector/PathAliasEntityCollector.php, line 34

Class

PathAliasEntityCollector

Namespace

Drupal\depcalc\EventSubscriber\DependencyCollector

Code

public function onCalculateDependencies(CalculateEntityDependenciesEvent $event) {

  // @todo remove version condition once 8.7 is no longer supported.
  if ($event
    ->getEntity()
    ->getEntityTypeId() === 'path_alias' && version_compare(\Drupal::VERSION, '8.8.0', '>=') && \Drupal::moduleHandler()
    ->moduleExists('path_alias')) {

    /** @var \Drupal\path_alias\Entity\PathAlias $entity */
    $entity = $event
      ->getEntity();
    $params = $this->matcher
      ->match($entity
      ->getPath());
    foreach ($params['_raw_variables']
      ->keys() as $parameter) {
      if (!empty($params[$parameter]) && $params[$parameter] instanceof EntityInterface) {
        $entity_wrapper = new DependentEntityWrapper($params[$parameter]);
        $entity_wrapper
          ->addDependency($event
          ->getWrapper(), $event
          ->getStack());
        $local_dependencies = [];
        $this
          ->mergeDependencies($entity_wrapper, $event
          ->getStack(), $this
          ->getCalculator()
          ->calculateDependencies($entity_wrapper, $event
          ->getStack(), $local_dependencies));
        $event
          ->addDependency($entity_wrapper);
      }
    }
  }
}