Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / modules / system / src / EventSubscriber / AdminRouteSubscriber.php
1 <?php
2
3 namespace Drupal\system\EventSubscriber;
4
5 use Drupal\Core\Routing\RouteSubscriberBase;
6 use Drupal\Core\Routing\RoutingEvents;
7 use Symfony\Component\Routing\Route;
8 use Symfony\Component\Routing\RouteCollection;
9
10 /**
11  * Adds the _admin_route option to each admin HTML route.
12  */
13 class AdminRouteSubscriber extends RouteSubscriberBase {
14
15   /**
16    * {@inheritdoc}
17    */
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);
22       }
23     }
24   }
25
26   /**
27    * {@inheritdoc}
28    */
29   public static function getSubscribedEvents() {
30     $events = parent::getSubscribedEvents();
31
32     // Use a lower priority than \Drupal\field_ui\Routing\RouteSubscriber or
33     // \Drupal\views\EventSubscriber\RouteSubscriber to ensure we add the option
34     // to their routes.
35     $events[RoutingEvents::ALTER] = ['onAlterRoutes', -200];
36
37     return $events;
38   }
39
40   /**
41    * Determines whether the given route is a HTML route.
42    *
43    * @param \Symfony\Component\Routing\Route $route
44    *   The route to analyze.
45    *
46    * @return bool
47    *   TRUE if HTML is a valid format for this route.
48    */
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);
53   }
54
55 }