comparison core/lib/Drupal/Core/Controller/TitleResolver.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 129ea1e6d783
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
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\Routing\Route;
9
10 /**
11 * Provides the default implementation of the title resolver interface.
12 */
13 class TitleResolver implements TitleResolverInterface {
14 use StringTranslationTrait;
15
16 /**
17 * The controller resolver.
18 *
19 * @var \Drupal\Core\Controller\ControllerResolverInterface
20 */
21 protected $controllerResolver;
22
23 /**
24 * Constructs a TitleResolver instance.
25 *
26 * @param \Drupal\Core\Controller\ControllerResolverInterface $controller_resolver
27 * The controller resolver.
28 * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
29 * The translation manager.
30 */
31 public function __construct(ControllerResolverInterface $controller_resolver, TranslationInterface $string_translation) {
32 $this->controllerResolver = $controller_resolver;
33 $this->stringTranslation = $string_translation;
34 }
35
36 /**
37 * {@inheritdoc}
38 */
39 public function getTitle(Request $request, Route $route) {
40 $route_title = NULL;
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);
48 }
49 elseif ($title = $route->getDefault('_title')) {
50 $options = [];
51 if ($context = $route->getDefault('_title_context')) {
52 $options['context'] = $context;
53 }
54 $args = [];
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;
59 }
60 }
61 if ($title_arguments = $route->getDefault('_title_arguments')) {
62 $args = array_merge($args, (array) $title_arguments);
63 }
64
65 // Fall back to a static string from the route.
66 $route_title = $this->t($title, $args, $options);
67 }
68 return $route_title;
69 }
70
71 }