You are here

class EntityRedirectCollector in Acquia Content Hub 8.2

Collector for collecting redirect entities for content entity.

@package Drupal\depcalc\EventSubscriber\DependencyCollector

Hierarchy

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

Expanded class hierarchy of EntityRedirectCollector

1 string reference to 'EntityRedirectCollector'
acquia_contenthub.services.yml in ./acquia_contenthub.services.yml
acquia_contenthub.services.yml
1 service uses EntityRedirectCollector
acquia_contenthub.entity.redirect.calculator in ./acquia_contenthub.services.yml
Drupal\acquia_contenthub\EventSubscriber\DependencyCollector\EntityRedirectCollector

File

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

Namespace

Drupal\acquia_contenthub\EventSubscriber\DependencyCollector
View source
class EntityRedirectCollector extends BaseDependencyCollector {

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

  /**
   * EntityRedirectCollector constructor.
   *
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   Module handler service.
   */
  public function __construct(ModuleHandlerInterface $module_handler) {
    $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('redirect')) {
      return;
    }
    $entity = $event
      ->getEntity();
    if ($entity
      ->getEntityTypeId() === 'redirect' || !$entity instanceof ContentEntityInterface) {
      return;
    }
    $redirects = $this
      ->getRedirectEntities($entity);
    if (!empty($redirects)) {
      $event
        ->getWrapper()
        ->addModuleDependencies([
        'redirect',
      ]);
      foreach ($redirects as $redirect) {
        $this
          ->addDependency($event, $redirect);
      }
    }
  }

  /**
   * Get list of redirect entities for given entity.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   Entity.
   *
   * @return \Drupal\redirect\Entity\Redirect[]
   *   Redirect list.
   */
  protected function getRedirectEntities(EntityInterface $entity) : array {

    /** @var \Drupal\redirect\RedirectRepository $redirect_service */
    $redirect_service = \Drupal::service('redirect.repository');
    try {
      $entity_url = "/{$entity->toUrl()->getInternalPath()}";
      $internal_entity_url = 'internal:' . $entity_url;
      $destination_urls = [
        $internal_entity_url,
      ];
      if ($this->moduleHandler
        ->moduleExists('path_alias')) {
        $alias = \Drupal::service('path_alias.manager')
          ->getAliasByPath($entity_url);
        $internal_alias_url = 'internal:' . $alias;
        $destination_urls = array_unique([
          $internal_alias_url,
          $internal_entity_url,
        ]);
      }
      $destination_urls[] = 'entity:' . $entity
        ->getEntityTypeId() . '/' . $entity
        ->id();
      return $redirect_service
        ->findByDestinationUri($destination_urls);
    } catch (UndefinedLinkTemplateException|\Exception $e) {
      return [];
    }
  }

  /**
   * Adds redirect as a dependency.
   *
   * @param \Drupal\depcalc\Event\CalculateEntityDependenciesEvent $event
   *   Event.
   * @param \Drupal\redirect\Entity\Redirect $redirect
   *   Redirect.
   *
   * @throws \Exception
   */
  protected function addDependency(CalculateEntityDependenciesEvent $event, Redirect $redirect) : void {
    $redirect_wrapper = new DependentEntityWrapper($redirect);
    $local_dependencies = [];
    $redirect_wrapper_dependencies = $this
      ->getCalculator()
      ->calculateDependencies($redirect_wrapper, $event
      ->getStack(), $local_dependencies);
    $this
      ->mergeDependencies($redirect_wrapper, $event
      ->getStack(), $redirect_wrapper_dependencies);
    $event
      ->addDependency($redirect_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.
EntityRedirectCollector::$moduleHandler protected property The module handler.
EntityRedirectCollector::addDependency protected function Adds redirect as a dependency.
EntityRedirectCollector::getRedirectEntities protected function Get list of redirect entities for given entity.
EntityRedirectCollector::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
EntityRedirectCollector::onCalculateDependencies public function Reacts on CALCULATE_DEPENDENCIES event.
EntityRedirectCollector::__construct public function EntityRedirectCollector constructor.