3 namespace Drupal\system\EventSubscriber;
5 use Drupal\Core\Routing\RouteSubscriberBase;
6 use Drupal\Core\Routing\RoutingEvents;
7 use Symfony\Component\Routing\Route;
8 use Symfony\Component\Routing\RouteCollection;
11 * Adds the _admin_route option to each admin HTML route.
13 class AdminRouteSubscriber extends RouteSubscriberBase {
18 protected function alterRoutes(RouteCollection $collection) {
19 foreach ($collection->all() as $route) {
20 if (strpos($route->getPath(), '/admin') === 0 && !$route->hasOption('_admin_route') && static::isHtmlRoute($route)) {
21 $route->setOption('_admin_route', TRUE);
29 public static function getSubscribedEvents() {
30 $events = parent::getSubscribedEvents();
32 // Use a lower priority than \Drupal\field_ui\Routing\RouteSubscriber or
33 // \Drupal\views\EventSubscriber\RouteSubscriber to ensure we add the option
35 $events[RoutingEvents::ALTER] = ['onAlterRoutes', -200];
41 * Determines whether the given route is a HTML route.
43 * @param \Symfony\Component\Routing\Route $route
44 * The route to analyze.
47 * TRUE if HTML is a valid format for this route.
49 protected static function isHtmlRoute(Route $route) {
50 // If a route has no explicit format, then HTML is valid.
51 $format = $route->hasRequirement('_format') ? explode('|', $route->getRequirement('_format')) : ['html'];
52 return in_array('html', $format, TRUE);