function TitleResolver::getTitle

Same name and namespace in other branches
  1. 10 core/lib/Drupal/Core/Controller/TitleResolver.php \Drupal\Core\Controller\TitleResolver::getTitle()
  2. 9 core/lib/Drupal/Core/Controller/TitleResolver.php \Drupal\Core\Controller\TitleResolver::getTitle()
  3. 8.9.x core/lib/Drupal/Core/Controller/TitleResolver.php \Drupal\Core\Controller\TitleResolver::getTitle()

Returns a static or dynamic title for the route.

If the returned title can contain HTML that should not be escaped it should return a render array, for example:

[
  '#markup' => 'title',
  '#allowed_tags' => [
    'em',
  ],
];

If the method returns a string and it is not marked safe then it will be auto-escaped.

Parameters

\Symfony\Component\HttpFoundation\Request $request: The request object passed to the title callback.

\Symfony\Component\Routing\Route $route: The route information of the route to fetch the title.

Return value

array|string|\Stringable|null The title for the route. The title for the route. NULL should be returned if the method can determine that the title will evaluate to an empty string.

Overrides TitleResolverInterface::getTitle

File

core/lib/Drupal/Core/Controller/TitleResolver.php, line 51

Class

TitleResolver
Provides the default implementation of the title resolver interface.

Namespace

Drupal\Core\Controller

Code

public function getTitle(Request $request, Route $route) {
  $route_title = NULL;
  // A dynamic title takes priority. Route::getDefault() returns NULL if the
  // named default is not set.  By testing the value directly, we also avoid
  // trying to use empty values.
  if ($callback = $route->getDefault('_title_callback')) {
    $callable = $this->controllerResolver
      ->getControllerFromDefinition($callback);
    $arguments = $this->argumentResolver
      ->getArguments($request, $callable);
    $route_title = call_user_func_array($callable, $arguments);
  }
  elseif ($route->hasDefault('_title') && strlen($route->getDefault('_title')) > 0) {
    $title = $route->getDefault('_title');
    $options = [];
    if ($route->hasDefault('_title_context')) {
      $options['context'] = $route->getDefault('_title_context');
    }
    $args = [];
    if ($route->hasDefault('_title_arguments')) {
      $args = (array) $route->getDefault('_title_arguments');
    }
    if ($raw_parameters = $request->attributes
      ->get('_raw_variables')) {
      foreach ($raw_parameters->all() as $key => $value) {
        if (is_scalar($value)) {
          $args['@' . $key] = $value;
          $args['%' . $key] = $value;
        }
      }
    }
    // Fall back to a static string from the route.
    // phpcs:ignore Drupal.Semantics.FunctionT.NotLiteralString
    $route_title = $this->t($title, $args, $options);
  }
  // Empty titles should return a NULL value as this is same result as title
  // not being set.
  if ($route_title === '' || $route_title instanceof TranslatableMarkup && $route_title->getUntranslatedString() === '') {
    return NULL;
  }
  return $route_title;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.