You are here

protected function PageManagerRoutes::alterRoutes in Page Manager 8.4

Same name and namespace in other branches
  1. 8 src/Routing/PageManagerRoutes.php \Drupal\page_manager\Routing\PageManagerRoutes::alterRoutes()

Alters existing routes for a specific collection.

Parameters

\Symfony\Component\Routing\RouteCollection $collection: The route collection for adding routes.

Overrides RouteSubscriberBase::alterRoutes

File

src/Routing/PageManagerRoutes.php, line 49

Class

PageManagerRoutes
Provides routes for page entities.

Namespace

Drupal\page_manager\Routing

Code

protected function alterRoutes(RouteCollection $collection) {
  foreach ($this->entityStorage
    ->loadMultiple() as $entity_id => $entity) {

    /** @var \Drupal\page_manager\PageInterface $entity */

    // If the page is disabled skip making a route for it.
    if (!$entity
      ->status() || !$entity
      ->getVariants()) {
      continue;
    }
    $defaults = [];
    $parameters = [];
    $requirements = [];
    $route_name = "page_manager.page_view_{$entity_id}";
    if ($overridden_route_name = $this
      ->findOverriddenRouteName($entity, $collection)) {
      $base_route_name = $overridden_route_name;
      $collection_route = $collection
        ->get($overridden_route_name);

      // Add the name of the overridden route for use during filtering.
      $defaults['overridden_route_name'] = $overridden_route_name;
      $path = $collection_route
        ->getPath();
      $parameters = $collection_route
        ->getOption('parameters') ?: [];
      $requirements = $collection_route
        ->getRequirements();
    }
    else {
      $base_route_name = $route_name;
      $path = $entity
        ->getPath();
    }

    // Add in configured parameters.
    foreach ($entity
      ->getParameters() as $parameter_name => $parameter) {
      if (!empty($parameter['type'])) {
        $parameters[$parameter_name]['type'] = $parameter['type'];
      }
    }

    // When adding multiple variants, the variant ID is added to the route
    // name. In order to convey the base route name for this set of variants,
    // add it as a parameter.
    $defaults['base_route_name'] = $base_route_name;
    $defaults['_entity_view'] = 'page_manager_page_variant';
    $defaults['_title'] = $entity
      ->label();
    $defaults['page_manager_page'] = $entity
      ->id();
    $parameters['page_manager_page_variant']['type'] = 'entity:page_variant';
    $parameters['page_manager_page']['type'] = 'entity:page';
    $requirements['_page_access'] = 'page_manager_page.view';
    foreach ($entity
      ->getVariants() as $variant_id => $variant) {

      // Construct and add a new route.
      $route = new Route($path, $defaults + [
        'page_manager_page_variant' => $variant_id,
        'page_manager_page_variant_weight' => $variant
          ->getWeight(),
      ], $requirements, [
        'parameters' => $parameters,
        '_admin_route' => $entity
          ->usesAdminTheme(),
      ]);
      $collection
        ->add($route_name . '_' . $variant_id, $route);
    }

    // Invalidate any page with the same base route name. See
    // \Drupal\page_manager\EventSubscriber\RouteNameResponseSubscriber.
    $this->cacheTagsInvalidator
      ->invalidateTags([
      "page_manager_route_name:{$base_route_name}",
    ]);
  }
}