Verified Commit f2cee08d authored by godotislate's avatar godotislate
Browse files

task: #3593789 Extend RouteSubscriberBase to define as well as alter routes

By: longwave
By: godotislate
By: dcam
(cherry picked from commit f6d327d1)
parent f5723cd5
Loading
Loading
Loading
Loading
+26 −2
Changes for core/lib/Drupal/Core/Routing/RouteSubscriberBase.php: 26 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -6,26 +6,50 @@
use Symfony\Component\Routing\RouteCollection;

/**
 * Provides a base implementation for RouteSubscriber.
 * Provides a base implementation for routing event subscribers.
 */
abstract class RouteSubscriberBase implements EventSubscriberInterface {

  /**
   * Provides new routes during the RoutingEvents::STATIC phase.
   *
   * This is the object-oriented equivalent of a 'route_callbacks' entry in a
   * *.routing.yml file.
   *
   * @return \Symfony\Component\Routing\RouteCollection
   *   A collection of new routes to add.
   */
  protected function buildRoutes(): RouteCollection {
    return new RouteCollection();
  }

  /**
   * Alters existing routes for a specific collection.
   *
   * @param \Symfony\Component\Routing\RouteCollection $collection
   *   The route collection for adding routes.
   */
  abstract protected function alterRoutes(RouteCollection $collection);
  protected function alterRoutes(RouteCollection $collection) {}

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents(): array {
    $events[RoutingEvents::STATIC] = 'onBuildRoutes';
    $events[RoutingEvents::ALTER] = 'onAlterRoutes';
    return $events;
  }

  /**
   * Delegates the route building to self::buildRoutes().
   *
   * @param \Drupal\Core\Routing\RouteBuildEvent $event
   *   The route build event.
   */
  public function onBuildRoutes(RouteBuildEvent $event): void {
    $event->getRouteCollection()->addCollection($this->buildRoutes());
  }

  /**
   * Delegates the route altering to self::alterRoutes().
   *
+13 −5
Changes for core/modules/views/src/EventSubscriber/RouteSubscriber.php: 13 added lines, 5 removed lines.
Original line number Diff line number Diff line
@@ -76,6 +76,7 @@ public function reset() {
  public static function getSubscribedEvents(): array {
    $events = parent::getSubscribedEvents();
    $events[RoutingEvents::FINISHED] = ['routeRebuildFinished'];
    $events[RoutingEvents::STATIC] = ['onBuildRoutes'];
    // Ensure to run after the entity resolver subscriber.
    // @see \Drupal\Core\EventSubscriber\EntityRouteAlterSubscriber
    $events[RoutingEvents::ALTER] = ['onAlterRoutes', -175];
@@ -102,12 +103,9 @@ protected function getViewsDisplayIDsWithRoute() {
  }

  /**
   * Returns a set of route objects.
   *
   * @return \Symfony\Component\Routing\RouteCollection
   *   A route collection.
   * {@inheritdoc}
   */
  public function routes() {
  protected function buildRoutes(): RouteCollection {
    $collection = new RouteCollection();
    foreach ($this->getViewsDisplayIDsWithRoute() as $pair) {
      [$view_id, $display_id] = explode('.', $pair);
@@ -127,6 +125,16 @@ public function routes() {
    return $collection;
  }

  /**
   * Returns a set of route objects.
   *
   * @return \Symfony\Component\Routing\RouteCollection
   *   A route collection.
   */
  public function routes() {
    return $this->buildRoutes();
  }

  /**
   * {@inheritdoc}
   */
+0 −2
Changes for core/modules/views/views.routing.yml: 0 added lines, 2 removed lines.
Original line number Diff line number Diff line
route_callbacks:
  - 'views.route_subscriber:routes'