0

what I'm trying to achieve is, for some specific nodes, to set the admin theme.

How can I get node information in this class?

class RouteSubscriber extends RouteSubscriberBase {

  /**
   * {@inheritdoc}
   */
  public function alterRoutes(RouteCollection $collection) {
    if ($route = $collection->get('entity.node.canonical')) {
      $node = \Drupal::routeMatch()->getParameter('node');
      // dpm($node); - this will be null
      if ($nid == 123) {
        $route->setOption('_node_operation_route', TRUE);
      }
    }
  }

}

$node = \Drupal::routeMatch()->getParameter('node'); does not work; $route->getPath() returns '/node/{node}'.

3
  • As you said $route->getPath() will return /node/{node} ,by using explode function you can get the node id as a array format ,or try in such way $entity = \Drupal::routeMatch()->getParameter('node'); if ($entity instanceof \Drupal\node\NodeInterface) { // Check if it is a node $nid = $node->id(); // You also have the node object right here }
    – Vikram8888
    Commented May 5, 2022 at 12:50
  • 2
    You'll never be able to get a node ID here - this event will only fire when caches are cleared or when a module is enabled.
    – sonfd
    Commented May 5, 2022 at 13:08
  • 4
    If you want to choose a different theme for specific nodes you need a custom theme negotiator. See drupal.stackexchange.com/questions/281447/…
    – 4uk4
    Commented May 5, 2022 at 13:43

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.