The route name of the current page is available in page.html.twig
?
The page is generated by the default feedback form.
To get the current route name, use:
$route_name = \Drupal::routeMatch()->getRouteName();
You can add the current page's route name as a variable in your theme's ".theme" file. Add a _preprocess_page function like this, and clear the drupal cache.
/**
* Implements hook_preprocess_page().
*
*/
function mytheme_preprocess_page(&$variables) {
$variables['route_name'] = \Drupal::routeMatch()->getRouteName();
}
Then you can access in page.html.twig like this:
{{ route_name }}
Note: \Drupal::routeMatch()->getRouteName()
will sometimes return null.
If you are inside a class, to do things properly, you'll want to inject the route match service in the constructor and then call it this way:
$this->currentRouteMatch->getRouteName()
The constructor (and variable) will be like this:
/**
* The current route match.
*
* @var \Drupal\Core\Routing\RouteMatchInterface
*/
protected $currentRouteMatch;
/**
* Constructs a new ThemeTestSubscriber.
*
* @param \Drupal\Core\Routing\RouteMatchInterface $current_route_match
*/
public function __construct(RouteMatchInterface $current_route_match) {
$this->currentRouteMatch = $current_route_match;
}
If it's a service class, you'd then pass it into the service in the yaml file in your custom module:
services:
mymodule.service:
class: Drupal\mymodule\MyCustomService
arguments: ['@current_route_match']
-
2There is a CLI (line mode command) somewhere that shows all drupal routes... can't seem to remember what it was... I'll update (unless someone else knows) – sea26.2 Aug 21 '19 at 16:33
-
@sea26.2 It use to be
drupal router:debug
:grin: but is nowdrupal debug:router
– Dan Shumaker Dec 15 '20 at 15:32