You are here

class AdminRouteSubscriber in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/system/src/EventSubscriber/AdminRouteSubscriber.php \Drupal\system\EventSubscriber\AdminRouteSubscriber
  2. 9 core/modules/system/src/EventSubscriber/AdminRouteSubscriber.php \Drupal\system\EventSubscriber\AdminRouteSubscriber

Adds the _admin_route option to each admin HTML route.

Hierarchy

Expanded class hierarchy of AdminRouteSubscriber

1 file declares its use of AdminRouteSubscriber
AdminRouteSubscriberTest.php in core/modules/system/tests/src/Unit/Routing/AdminRouteSubscriberTest.php
1 string reference to 'AdminRouteSubscriber'
system.services.yml in core/modules/system/system.services.yml
core/modules/system/system.services.yml
1 service uses AdminRouteSubscriber
system.admin_path.route_subscriber in core/modules/system/system.services.yml
Drupal\system\EventSubscriber\AdminRouteSubscriber

File

core/modules/system/src/EventSubscriber/AdminRouteSubscriber.php, line 13

Namespace

Drupal\system\EventSubscriber
View source
class AdminRouteSubscriber extends RouteSubscriberBase {

  /**
   * {@inheritdoc}
   */
  protected function alterRoutes(RouteCollection $collection) {
    foreach ($collection
      ->all() as $route) {
      $path = $route
        ->getPath();
      if (($path == '/admin' || strpos($path, '/admin/') === 0) && !$route
        ->hasOption('_admin_route') && static::isHtmlRoute($route)) {
        $route
          ->setOption('_admin_route', TRUE);
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() : array {
    $events = parent::getSubscribedEvents();

    // Use a lower priority than \Drupal\field_ui\Routing\RouteSubscriber or
    // \Drupal\views\EventSubscriber\RouteSubscriber to ensure we add the option
    // to their routes.
    $events[RoutingEvents::ALTER] = [
      'onAlterRoutes',
      -200,
    ];
    return $events;
  }

  /**
   * Determines whether the given route is an HTML route.
   *
   * @param \Symfony\Component\Routing\Route $route
   *   The route to analyze.
   *
   * @return bool
   *   TRUE if HTML is a valid format for this route.
   */
  protected static function isHtmlRoute(Route $route) {

    // If a route has no explicit format, then HTML is valid.
    $format = $route
      ->hasRequirement('_format') ? explode('|', $route
      ->getRequirement('_format')) : [
      'html',
    ];
    return in_array('html', $format, TRUE);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AdminRouteSubscriber::alterRoutes protected function Alters existing routes for a specific collection. Overrides RouteSubscriberBase::alterRoutes
AdminRouteSubscriber::getSubscribedEvents public static function Overrides RouteSubscriberBase::getSubscribedEvents
AdminRouteSubscriber::isHtmlRoute protected static function Determines whether the given route is an HTML route.
RouteSubscriberBase::onAlterRoutes public function Delegates the route altering to self::alterRoutes().