Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\node\EventSubscriber;
|
Chris@0
|
4
|
Chris@18
|
5 use Drupal\Core\Config\ConfigCrudEvent;
|
Chris@18
|
6 use Drupal\Core\Config\ConfigEvents;
|
Chris@0
|
7 use Drupal\Core\Config\ConfigFactoryInterface;
|
Chris@18
|
8 use Drupal\Core\Routing\RouteBuilderInterface;
|
Chris@0
|
9 use Drupal\Core\Routing\RouteSubscriberBase;
|
Chris@0
|
10 use Symfony\Component\Routing\RouteCollection;
|
Chris@0
|
11
|
Chris@0
|
12 /**
|
Chris@0
|
13 * Sets the _admin_route for specific node-related routes.
|
Chris@0
|
14 */
|
Chris@0
|
15 class NodeAdminRouteSubscriber extends RouteSubscriberBase {
|
Chris@0
|
16
|
Chris@0
|
17 /**
|
Chris@0
|
18 * The config factory.
|
Chris@0
|
19 *
|
Chris@0
|
20 * @var \Drupal\Core\Config\ConfigFactoryInterface
|
Chris@0
|
21 */
|
Chris@0
|
22 protected $configFactory;
|
Chris@0
|
23
|
Chris@0
|
24 /**
|
Chris@18
|
25 * The router builder.
|
Chris@18
|
26 *
|
Chris@18
|
27 * @var \Drupal\Core\Routing\RouteBuilderInterface
|
Chris@18
|
28 */
|
Chris@18
|
29 protected $routerBuilder;
|
Chris@18
|
30
|
Chris@18
|
31 /**
|
Chris@0
|
32 * Constructs a new NodeAdminRouteSubscriber.
|
Chris@0
|
33 *
|
Chris@0
|
34 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
|
Chris@0
|
35 * The config factory.
|
Chris@18
|
36 * @param \Drupal\Core\Routing\RouteBuilderInterface $router_builder
|
Chris@18
|
37 * The router builder service.
|
Chris@0
|
38 */
|
Chris@18
|
39 public function __construct(ConfigFactoryInterface $config_factory, RouteBuilderInterface $router_builder) {
|
Chris@0
|
40 $this->configFactory = $config_factory;
|
Chris@18
|
41 $this->routerBuilder = $router_builder;
|
Chris@0
|
42 }
|
Chris@0
|
43
|
Chris@0
|
44 /**
|
Chris@0
|
45 * {@inheritdoc}
|
Chris@0
|
46 */
|
Chris@0
|
47 protected function alterRoutes(RouteCollection $collection) {
|
Chris@0
|
48 if ($this->configFactory->get('node.settings')->get('use_admin_theme')) {
|
Chris@0
|
49 foreach ($collection->all() as $route) {
|
Chris@0
|
50 if ($route->hasOption('_node_operation_route')) {
|
Chris@0
|
51 $route->setOption('_admin_route', TRUE);
|
Chris@0
|
52 }
|
Chris@0
|
53 }
|
Chris@0
|
54 }
|
Chris@0
|
55 }
|
Chris@0
|
56
|
Chris@18
|
57 /**
|
Chris@18
|
58 * Rebuilds the router when node.settings:use_admin_theme is changed.
|
Chris@18
|
59 *
|
Chris@18
|
60 * @param \Drupal\Core\Config\ConfigCrudEvent $event
|
Chris@18
|
61 */
|
Chris@18
|
62 public function onConfigSave(ConfigCrudEvent $event) {
|
Chris@18
|
63 if ($event->getConfig()->getName() === 'node.settings' && $event->isChanged('use_admin_theme')) {
|
Chris@18
|
64 $this->routerBuilder->setRebuildNeeded();
|
Chris@18
|
65 }
|
Chris@18
|
66 }
|
Chris@18
|
67
|
Chris@18
|
68 /**
|
Chris@18
|
69 * {@inheritdoc}
|
Chris@18
|
70 */
|
Chris@18
|
71 public static function getSubscribedEvents() {
|
Chris@18
|
72 $events = parent::getSubscribedEvents();
|
Chris@18
|
73 $events[ConfigEvents::SAVE][] = ['onConfigSave', 0];
|
Chris@18
|
74 return $events;
|
Chris@18
|
75 }
|
Chris@18
|
76
|
Chris@0
|
77 }
|