class AdminRouteSubscriber in Drupal 9
Same name and namespace in other branches
- 8 core/modules/system/src/EventSubscriber/AdminRouteSubscriber.php \Drupal\system\EventSubscriber\AdminRouteSubscriber
- 10 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
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
File
- core/
modules/ system/ src/ EventSubscriber/ AdminRouteSubscriber.php, line 13
Namespace
Drupal\system\EventSubscriberView 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() {
$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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AdminRouteSubscriber:: |
protected | function |
Alters existing routes for a specific collection. Overrides RouteSubscriberBase:: |
|
AdminRouteSubscriber:: |
public static | function |
Overrides RouteSubscriberBase:: |
|
AdminRouteSubscriber:: |
protected static | function | Determines whether the given route is an HTML route. | |
RouteSubscriberBase:: |
public | function | Delegates the route altering to self::alterRoutes(). | 1 |