16

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

1

5 Answers 5

25

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:

drupal_get_title()

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

<?php
  $title = drupal_get_title();
?>

Drupal 8

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

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.

3
  • +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. Commented Nov 11, 2020 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
    Commented Nov 11, 2020 at 17:36
  • FYI - This approach doesn’t work on node edit pages.
    – dcmouyard
    Commented Nov 3, 2021 at 22:48
3

Note

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.

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

Drupal 8

$request = \Drupal::request();
  // Assuming the Request is $request.
  if ($request->attributes->has('_title')) {
    return $request->attributes->get('_title');
  }
1
  • 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
    Commented Jul 19, 2017 at 15:28
-3

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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