Drupal - How to get the front page title in page.html.twig?

You should really try using the blocks for this, but here's how to do what you asked:

function YOUR_THEME_preprocess_page(&$variables) {
  $request = \Drupal::request();
  $route_match = \Drupal::routeMatch();
  $page_title = \Drupal::service('title_resolver')->getTitle($request, $route_match->getRouteObject());
  $variables['current_page_title'] = $page_title;
}

This code should go in YOUR_THEME.theme file. You can use it then in page.html.twig this way:

{{ current_page_title }}

See this for more information.


{{ page['#title'] }}

It seems to work on view, node and term pages.

Tags:

8