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