AdminRouteSubscriber.php in Drupal 9
File
core/modules/system/src/EventSubscriber/AdminRouteSubscriber.php
View source
<?php
namespace Drupal\system\EventSubscriber;
use Drupal\Core\Routing\RouteSubscriberBase;
use Drupal\Core\Routing\RoutingEvents;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
class AdminRouteSubscriber extends RouteSubscriberBase {
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);
}
}
}
public static function getSubscribedEvents() {
$events = parent::getSubscribedEvents();
$events[RoutingEvents::ALTER] = [
'onAlterRoutes',
-200,
];
return $events;
}
protected static function isHtmlRoute(Route $route) {
$format = $route
->hasRequirement('_format') ? explode('|', $route
->getRequirement('_format')) : [
'html',
];
return in_array('html', $format, TRUE);
}
}