3 namespace Drupal\Core\Controller;
5 use Drupal\Core\StringTranslation\StringTranslationTrait;
6 use Drupal\Core\StringTranslation\TranslationInterface;
7 use Symfony\Component\HttpFoundation\Request;
8 use Symfony\Component\Routing\Route;
11 * Provides the default implementation of the title resolver interface.
13 class TitleResolver implements TitleResolverInterface {
14 use StringTranslationTrait;
17 * The controller resolver.
19 * @var \Drupal\Core\Controller\ControllerResolverInterface
21 protected $controllerResolver;
24 * Constructs a TitleResolver instance.
26 * @param \Drupal\Core\Controller\ControllerResolverInterface $controller_resolver
27 * The controller resolver.
28 * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
29 * The translation manager.
31 public function __construct(ControllerResolverInterface $controller_resolver, TranslationInterface $string_translation) {
32 $this->controllerResolver = $controller_resolver;
33 $this->stringTranslation = $string_translation;
39 public function getTitle(Request $request, Route $route) {
41 // A dynamic title takes priority. Route::getDefault() returns NULL if the
42 // named default is not set. By testing the value directly, we also avoid
43 // trying to use empty values.
44 if ($callback = $route->getDefault('_title_callback')) {
45 $callable = $this->controllerResolver->getControllerFromDefinition($callback);
46 $arguments = $this->controllerResolver->getArguments($request, $callable);
47 $route_title = call_user_func_array($callable, $arguments);
49 elseif ($title = $route->getDefault('_title')) {
51 if ($context = $route->getDefault('_title_context')) {
52 $options['context'] = $context;
55 if (($raw_parameters = $request->attributes->get('_raw_variables'))) {
56 foreach ($raw_parameters->all() as $key => $value) {
57 $args['@' . $key] = $value;
58 $args['%' . $key] = $value;
61 if ($title_arguments = $route->getDefault('_title_arguments')) {
62 $args = array_merge($args, (array) $title_arguments);
65 // Fall back to a static string from the route.
66 $route_title = $this->t($title, $args, $options);