$route_match = \Drupal::routeMatch(); if ($route_match->getRouteName() == 'entity.node.canonical') { return true; } // or \Drupal::request()->attributes->get('node');