You are here

public function TitleResolver::getTitle in Drupal 8

Same name and namespace in other branches
  1. 9 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|null The title for the route.

Overrides TitleResolverInterface::getTitle

File

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

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 ($title = $route
    ->getDefault('_title')) {
    $options = [];
    if ($context = $route
      ->getDefault('_title_context')) {
      $options['context'] = $context;
    }
    $args = [];
    if ($raw_parameters = $request->attributes
      ->get('_raw_variables')) {
      foreach ($raw_parameters
        ->all() as $key => $value) {
        $args['@' . $key] = $value;
        $args['%' . $key] = $value;
      }
    }
    if ($title_arguments = $route
      ->getDefault('_title_arguments')) {
      $args = array_merge($args, (array) $title_arguments);
    }

    // Fall back to a static string from the route.
    $route_title = $this
      ->t($title, $args, $options);
  }
  return $route_title;
}