You are here

class AdminRouteSubscriber in Drupal 8

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

Adds the _admin_route option to each admin HTML route.


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' in core/modules/system/
1 service uses AdminRouteSubscriber
system.admin_path.route_subscriber in core/modules/system/


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


View source
class AdminRouteSubscriber extends RouteSubscriberBase {

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

   * {@inheritdoc}
  public static function getSubscribedEvents() {
    $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] = [
    return $events;

   * Determines whether the given route is a 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')) : [
    return in_array('html', $format, TRUE);



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 Returns an array of event names this subscriber wants to listen to. Overrides RouteSubscriberBase::getSubscribedEvents
AdminRouteSubscriber::isHtmlRoute protected static function Determines whether the given route is a HTML route.
RouteSubscriberBase::onAlterRoutes public function Delegates the route altering to self::alterRoutes(). 1