Drupal 10: Load The Title Of A Page From The Request

This will use the current request object to find the title of the current page using the title_resolver service.

use \Symfony\Cmf\Component\Routing\RouteObjectInterface;
use \Symfony\Component\Routing\Route;

/** @var \Symfony\Component\HttpFoundation\Request $request */
$request = \Drupal::service('request_stack')->getCurrentRequest();

$route = $request->attributes->get(RouteObjectInterface::ROUTE_OBJECT);

if ($route instanceof Route) {
  $title = \Drupal::service('title_resolver')->getTitle($request, $route);
}

 

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
4 + 10 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.