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\HttpKernel\Controller\ArgumentResolverInterface;
9 use Symfony\Component\Routing\Route;
12 * Provides the default implementation of the title resolver interface.
14 class TitleResolver implements TitleResolverInterface {
15 use StringTranslationTrait;
18 * The controller resolver.
20 * @var \Drupal\Core\Controller\ControllerResolverInterface
22 protected $controllerResolver;
25 * The argument resolver.
27 * @var \Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface
29 protected $argumentResolver;
32 * Constructs a TitleResolver instance.
34 * @param \Drupal\Core\Controller\ControllerResolverInterface $controller_resolver
35 * The controller resolver.
36 * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
37 * The translation manager.
38 * @param \Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface $argument_resolver
39 * The argument resolver.
41 public function __construct(ControllerResolverInterface $controller_resolver, TranslationInterface $string_translation, ArgumentResolverInterface $argument_resolver) {
42 $this->controllerResolver = $controller_resolver;
43 $this->stringTranslation = $string_translation;
44 $this->argumentResolver = $argument_resolver;
50 public function getTitle(Request $request, Route $route) {
52 // A dynamic title takes priority. Route::getDefault() returns NULL if the
53 // named default is not set. By testing the value directly, we also avoid
54 // trying to use empty values.
55 if ($callback = $route->getDefault('_title_callback')) {
56 $callable = $this->controllerResolver->getControllerFromDefinition($callback);
57 $arguments = $this->argumentResolver->getArguments($request, $callable);
58 $route_title = call_user_func_array($callable, $arguments);
60 elseif ($title = $route->getDefault('_title')) {
62 if ($context = $route->getDefault('_title_context')) {
63 $options['context'] = $context;
66 if (($raw_parameters = $request->attributes->get('_raw_variables'))) {
67 foreach ($raw_parameters->all() as $key => $value) {
68 $args['@' . $key] = $value;
69 $args['%' . $key] = $value;
72 if ($title_arguments = $route->getDefault('_title_arguments')) {
73 $args = array_merge($args, (array) $title_arguments);
76 // Fall back to a static string from the route.
77 $route_title = $this->t($title, $args, $options);