drupal_get_title() has been removed from Drupal 8. How I can get the page title in Drupal 8?


If you check the change records you should find the following: drupal_set_title() and drupal_get_title() were removed.

You'll have to read the change notice for all of the info, but here is a starting point:


As titles on routes now can be set on various ways (see above), drupal_get_title() has been removed. In its place you should call the title_resolver service.

Drupal 7

  $title = drupal_get_title();

Drupal 8

  $request = \Drupal::request();
  if ($route = $request->attributes->get(\Symfony\Cmf\Component\Routing\RouteObjectInterface::ROUTE_OBJECT)) {
    $title = \Drupal::service('title_resolver')->getTitle($request, $route);
  • 2
    The accepted response is out of date: see elsewhere in this thread for CurrentRouteMatch. – J-P Jul 19 '17 at 15:45

As per batch_test.module (D8.4) the following should work just fine:

$request     = \Drupal::request();
$route_match = \Drupal::routeMatch();
$title       = \Drupal::service('title_resolver')->getTitle($request, $route_match->getRouteObject());

For more info see public function TitleResolver::getTitle.

  • +1 Confirming this works with Drupal 9 (9.0.7), with node-based pages at least. I'd expect it would work with non-node pages too but I haven't tried that yet. – therobyouknow Nov 11 '20 at 17:11
  • 1
    @therobyouknow – You are welcome my friend! BTW I noticed you tagged the question with and additional version tag. Maybe read drupal.meta.stackexchange.com/q/3640/15055 as an introduction regarding version tag usage and please try to avoid multiple version tags on the same post. Rather use none. – leymannx Nov 11 '20 at 17:36
  • +1 thank you @leymannx – therobyouknow Nov 11 '20 at 17:39


The accepted response for this question is out of date: See Request parameters, route name, and route object should be accessed via RouteMatch.

You should use the current_route_match service to retrieve the current route, and then read or modify its parameters. This wraps any Symfony etc. magic.

In procedural code, you would use code similar to the following one.

$route = \Drupal::routeMatch()->getCurrentRouteMatch()->getRouteObject();
$title = $route->getDefault('_title');

In object-oriented code, you should use dependency injection and inject the service.

  • What to do if there is no $route->getDefault('_title'); but only a $route->getDefault('_title_callback');? – leymannx Nov 14 '17 at 10:38
  • For example on example.com/user/1 – leymannx Nov 14 '17 at 10:52
  • 2
    This bypasses the TitleResolver so doesn't handle all situations. api.drupal.org/api/drupal/… – imclean May 3 '19 at 4:06
  • Using this solution will get the untranslated title. – Saud Alfadhli Oct 13 '19 at 9:27

In drupal 8 you can use $node->getTitle().

  • 1
    The issue with this answer is that the page you're on isn't necessarily a node. – Matt Aug 27 '20 at 22:15

Drupal 8

$request = \Drupal::request();
  // Assuming the Request is $request.
  if ($request->attributes->has('_title')) {
    return $request->attributes->get('_title');
  • I didn't mark this response down, but it looks like the ->get('_MAGIC') method is unfortunately deprecated and mostly removed from D8: drupal.org/node/2124749 – J-P Jul 19 '17 at 15:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.