class NodeAdminRouteSubscriber

Same name in other branches
  1. 9 core/modules/node/src/EventSubscriber/NodeAdminRouteSubscriber.php \Drupal\node\EventSubscriber\NodeAdminRouteSubscriber
  2. 10 core/modules/node/src/EventSubscriber/NodeAdminRouteSubscriber.php \Drupal\node\EventSubscriber\NodeAdminRouteSubscriber
  3. 11.x core/modules/node/src/EventSubscriber/NodeAdminRouteSubscriber.php \Drupal\node\EventSubscriber\NodeAdminRouteSubscriber

Sets the _admin_route for specific node-related routes.

Hierarchy

  • class \Drupal\Core\Routing\RouteSubscriberBase implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of NodeAdminRouteSubscriber

1 string reference to 'NodeAdminRouteSubscriber'
node.services.yml in core/modules/node/node.services.yml
core/modules/node/node.services.yml
1 service uses NodeAdminRouteSubscriber
node.admin_path.route_subscriber in core/modules/node/node.services.yml
Drupal\node\EventSubscriber\NodeAdminRouteSubscriber

File

core/modules/node/src/EventSubscriber/NodeAdminRouteSubscriber.php, line 15

Namespace

Drupal\node\EventSubscriber
View source
class NodeAdminRouteSubscriber extends RouteSubscriberBase {
    
    /**
     * The config factory.
     *
     * @var \Drupal\Core\Config\ConfigFactoryInterface
     */
    protected $configFactory;
    
    /**
     * The router builder.
     *
     * @var \Drupal\Core\Routing\RouteBuilderInterface
     */
    protected $routerBuilder;
    
    /**
     * Constructs a new NodeAdminRouteSubscriber.
     *
     * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
     *   The config factory.
     * @param \Drupal\Core\Routing\RouteBuilderInterface $router_builder
     *   The router builder service.
     */
    public function __construct(ConfigFactoryInterface $config_factory, RouteBuilderInterface $router_builder) {
        $this->configFactory = $config_factory;
        $this->routerBuilder = $router_builder;
    }
    
    /**
     * {@inheritdoc}
     */
    protected function alterRoutes(RouteCollection $collection) {
        if ($this->configFactory
            ->get('node.settings')
            ->get('use_admin_theme')) {
            foreach ($collection->all() as $route) {
                if ($route->hasOption('_node_operation_route')) {
                    $route->setOption('_admin_route', TRUE);
                }
            }
        }
    }
    
    /**
     * Rebuilds the router when node.settings:use_admin_theme is changed.
     *
     * @param \Drupal\Core\Config\ConfigCrudEvent $event
     */
    public function onConfigSave(ConfigCrudEvent $event) {
        if ($event->getConfig()
            ->getName() === 'node.settings' && $event->isChanged('use_admin_theme')) {
            $this->routerBuilder
                ->setRebuildNeeded();
        }
    }
    
    /**
     * {@inheritdoc}
     */
    public static function getSubscribedEvents() {
        $events = parent::getSubscribedEvents();
        $events[ConfigEvents::SAVE][] = [
            'onConfigSave',
            0,
        ];
        return $events;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
NodeAdminRouteSubscriber::$configFactory protected property The config factory.
NodeAdminRouteSubscriber::$routerBuilder protected property The router builder.
NodeAdminRouteSubscriber::alterRoutes protected function Alters existing routes for a specific collection. Overrides RouteSubscriberBase::alterRoutes
NodeAdminRouteSubscriber::getSubscribedEvents public static function Overrides RouteSubscriberBase::getSubscribedEvents
NodeAdminRouteSubscriber::onConfigSave public function Rebuilds the router when node.settings:use_admin_theme is changed.
NodeAdminRouteSubscriber::__construct public function Constructs a new NodeAdminRouteSubscriber.
RouteSubscriberBase::onAlterRoutes public function Delegates the route altering to self::alterRoutes(). 1

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.