You are here

class PreviewLinkRoutes in Preview Link 2.0.x

Same name and namespace in other branches
  1. 2.x src/Routing/PreviewLinkRoutes.php \Drupal\preview_link\Routing\PreviewLinkRoutes

Alters routes to add access checker to canonical entity type routes.

Checker is used to redirect users to preview link route.

Hierarchy

Expanded class hierarchy of PreviewLinkRoutes

1 string reference to 'PreviewLinkRoutes'
preview_link.services.yml in ./preview_link.services.yml
preview_link.services.yml
1 service uses PreviewLinkRoutes
preview_link.route_subscriber in ./preview_link.services.yml
Drupal\preview_link\Routing\PreviewLinkRoutes

File

src/Routing/PreviewLinkRoutes.php, line 18

Namespace

Drupal\preview_link\Routing
View source
class PreviewLinkRoutes extends RouteSubscriberBase {

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

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

  /**
   * {@inheritdoc}
   */
  protected function alterRoutes(RouteCollection $collection) : void {
    $entityTypes = $this->entityTypeManager
      ->getDefinitions();
    $entityTypes = array_filter($entityTypes, [
      PreviewLinkUtility::class,
      'isEntityTypeSupported',
    ]);
    foreach ($entityTypes as $entityType) {
      $canonicalPath = $entityType
        ->getLinkTemplate('canonical');
      $route = $this
        ->getRouteForPath($collection, $canonicalPath);
      if (!$route) {
        continue;
      }

      // Find the first parameter name for this entity type, otherwise fall back
      // to entity type ID, e.g. for block_content.
      $entityParameterName = $entityType
        ->id();
      foreach ($route
        ->getOptions()['parameters'] ?? [] as $parameterName => $value) {
        if (($value['type'] ?? NULL) === 'entity:' . $entityType
          ->id()) {
          $entityParameterName = $parameterName;
        }
      }
      if (strpos($route
        ->getPath(), sprintf('{%s}', $entityParameterName)) === FALSE) {
        throw new \LogicException(sprintf('Unable to determine parameter name representing an upcast of entity type `%s`', $entityType
          ->id()));
      }

      // Adds the parameter name as the value.
      $route
        ->addRequirements([
        '_access_preview_link_canonical_rerouter' => $entityParameterName,
      ]);
    }
  }

  /**
   * Determines the route for a path.
   *
   * @param \Symfony\Component\Routing\RouteCollection $collection
   *   The route collection.
   * @param string $path
   *   A route path.
   *
   * @return \Symfony\Component\Routing\Route|null
   *   The route, or NULL if no route found with this path in the collection.
   */
  protected function getRouteForPath(RouteCollection $collection, string $path) : ?Route {
    foreach ($collection as $route) {
      assert($route instanceof Route);
      if ($route
        ->getPath() === $path) {
        return $route;
      }
    }
    return NULL;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PreviewLinkRoutes::$entityTypeManager protected property Entity type manager.
PreviewLinkRoutes::alterRoutes protected function Alters existing routes for a specific collection. Overrides RouteSubscriberBase::alterRoutes
PreviewLinkRoutes::getRouteForPath protected function Determines the route for a path.
PreviewLinkRoutes::__construct public function PreviewLinkRoutes constructor.
RouteSubscriberBase::getSubscribedEvents public static function 7
RouteSubscriberBase::onAlterRoutes public function Delegates the route altering to self::alterRoutes(). 1