You are here

class EntityPathAliasCollector in Dependency Calculation 8

Hierarchy

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

Expanded class hierarchy of EntityPathAliasCollector

1 string reference to 'EntityPathAliasCollector'
depcalc.services.yml in ./depcalc.services.yml
depcalc.services.yml
1 service uses EntityPathAliasCollector
entity.path_alias.dependency.calculator in ./depcalc.services.yml
Drupal\depcalc\EventSubscriber\DependencyCollector\EntityPathAliasCollector

File

src/EventSubscriber/DependencyCollector/EntityPathAliasCollector.php, line 11

Namespace

Drupal\depcalc\EventSubscriber\DependencyCollector
View source
class EntityPathAliasCollector extends BaseDependencyCollector {
  public static function getSubscribedEvents() {
    $events = [];
    $events[DependencyCalculatorEvents::CALCULATE_DEPENDENCIES][] = [
      'onCalculateDependencies',
    ];
    return $events;
  }
  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')) {
      $entity = $event
        ->getEntity();
      try {
        $uri = "/{$entity->toUrl()->getInternalPath()}";

        /** @var \Drupal\path_alias\PathAliasStorage $storage */
        $storage = \Drupal::entityTypeManager()
          ->getStorage('path_alias');
        $paths = $storage
          ->loadByProperties([
          'path' => $uri,
        ]);
        if ($paths) {
          foreach ($paths as $path) {
            $path_wrapper = new DependentEntityWrapper($path);
            $path_wrapper
              ->addDependency($event
              ->getWrapper(), $event
              ->getStack());
            $local_dependencies = [];
            $this
              ->mergeDependencies($path_wrapper, $event
              ->getStack(), $this
              ->getCalculator()
              ->calculateDependencies($path_wrapper, $event
              ->getStack(), $local_dependencies));
            $event
              ->addDependency($path_wrapper);
          }
        }
      } catch (EntityMalformedException|UndefinedLinkTemplateException $e) {
        return;
      }
    }
  }

}

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.
EntityPathAliasCollector::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
EntityPathAliasCollector::onCalculateDependencies public function