El nombre de la ruta de la página actual está disponible en page.html.twig? La página se genera mediante el formulario de comentarios predeterminado.
24
Para obtener el nombre de la ruta actual, use:
$route_name = \Drupal::routeMatch()->getRouteName();
Puede agregar el nombre de ruta de la página actual como una variable en el archivo ".theme" de su tema. Agregue una función _preprocess_page como esta y borre el caché de drupal.
/**
* Implements hook_preprocess_page().
*
*/
function mytheme_preprocess_page(&$variables) {
$variables['route_name'] = \Drupal::routeMatch()->getRouteName();
}
Luego puede acceder en page.html.twig de esta manera:
{{ route_name }}
Nota: \Drupal::routeMatch()->getRouteName() a veces devolverá nulo.
Si está dentro de una clase, para hacer las cosas correctamente, querrá inyectar el servicio de coincidencia de ruta en el constructor y luego llamarlo de esta manera:
$this->currentRouteMatch->getRouteName()
El constructor (y variable) será así:
/**
* 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;
}
Si se trata de una clase de servicio, la pasaría al servicio en el archivo yaml de su módulo personalizado:
services:
mymodule.service:
class: Drupal\mymodule\MyCustomService
arguments: ['@current_route_match']