blob: 0e3e7a3ec31f44cd1882e14b65094e8c6d8321df (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
<?php
namespace Drupal\Core\Controller;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface;
use Symfony\Component\Routing\Route;
/**
* Provides the default implementation of the title resolver interface.
*/
class TitleResolver implements TitleResolverInterface {
use StringTranslationTrait;
/**
* The controller resolver.
*
* @var \Drupal\Core\Controller\ControllerResolverInterface
*/
protected $controllerResolver;
/**
* The argument resolver.
*
* @var \Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface
*/
protected $argumentResolver;
/**
* Constructs a TitleResolver instance.
*
* @param \Drupal\Core\Controller\ControllerResolverInterface $controller_resolver
* The controller resolver.
* @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
* The translation manager.
* @param \Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface $argument_resolver
* The argument resolver.
*/
public function __construct(ControllerResolverInterface $controller_resolver, TranslationInterface $string_translation, ArgumentResolverInterface $argument_resolver) {
$this->controllerResolver = $controller_resolver;
$this->stringTranslation = $string_translation;
$this->argumentResolver = $argument_resolver;
}
/**
* {@inheritdoc}
*/
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);
}
return $route_title;
}
}
|