Pull merge.
[yaffs-website] / web / core / lib / Drupal / Core / Controller / TitleResolver.php
1 <?php
2
3 namespace Drupal\Core\Controller;
4
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;
10
11 /**
12  * Provides the default implementation of the title resolver interface.
13  */
14 class TitleResolver implements TitleResolverInterface {
15   use StringTranslationTrait;
16
17   /**
18    * The controller resolver.
19    *
20    * @var \Drupal\Core\Controller\ControllerResolverInterface
21    */
22   protected $controllerResolver;
23
24   /**
25    * The argument resolver.
26    *
27    * @var \Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface
28    */
29   protected $argumentResolver;
30
31   /**
32    * Constructs a TitleResolver instance.
33    *
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.
40    */
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;
45   }
46
47   /**
48    * {@inheritdoc}
49    */
50   public function getTitle(Request $request, Route $route) {
51     $route_title = NULL;
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);
59     }
60     elseif ($title = $route->getDefault('_title')) {
61       $options = [];
62       if ($context = $route->getDefault('_title_context')) {
63         $options['context'] = $context;
64       }
65       $args = [];
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;
70         }
71       }
72       if ($title_arguments = $route->getDefault('_title_arguments')) {
73         $args = array_merge($args, (array) $title_arguments);
74       }
75
76       // Fall back to a static string from the route.
77       $route_title = $this->t($title, $args, $options);
78     }
79     return $route_title;
80   }
81
82 }