class AdminRouteSubscriber

Same name in other branches
  1. 9 core/modules/system/src/EventSubscriber/AdminRouteSubscriber.php \Drupal\system\EventSubscriber\AdminRouteSubscriber
  2. 8.9.x core/modules/system/src/EventSubscriber/AdminRouteSubscriber.php \Drupal\system\EventSubscriber\AdminRouteSubscriber
  3. 11.x core/modules/system/src/EventSubscriber/AdminRouteSubscriber.php \Drupal\system\EventSubscriber\AdminRouteSubscriber

Adds the _admin_route option to each admin HTML route.

Hierarchy

  • class \Drupal\Core\Routing\RouteSubscriberBase implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
    • class \Drupal\system\EventSubscriber\AdminRouteSubscriber extends \Drupal\Core\Routing\RouteSubscriberBase

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' || str_starts_with($path, '/admin/')) && !$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

Title Sort descending Modifiers Object type Summary Overriden Title 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(). 1

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.