TitleResolver.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace Drupal\Core\Controller;
  3. use Drupal\Core\StringTranslation\StringTranslationTrait;
  4. use Drupal\Core\StringTranslation\TranslationInterface;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface;
  7. use Symfony\Component\Routing\Route;
  8. /**
  9. * Provides the default implementation of the title resolver interface.
  10. */
  11. class TitleResolver implements TitleResolverInterface {
  12. use StringTranslationTrait;
  13. /**
  14. * The controller resolver.
  15. *
  16. * @var \Drupal\Core\Controller\ControllerResolverInterface
  17. */
  18. protected $controllerResolver;
  19. /**
  20. * The argument resolver.
  21. *
  22. * @var \Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface
  23. */
  24. protected $argumentResolver;
  25. /**
  26. * Constructs a TitleResolver instance.
  27. *
  28. * @param \Drupal\Core\Controller\ControllerResolverInterface $controller_resolver
  29. * The controller resolver.
  30. * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
  31. * The translation manager.
  32. * @param \Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface $argument_resolver
  33. * The argument resolver.
  34. */
  35. public function __construct(ControllerResolverInterface $controller_resolver, TranslationInterface $string_translation, ArgumentResolverInterface $argument_resolver) {
  36. $this->controllerResolver = $controller_resolver;
  37. $this->stringTranslation = $string_translation;
  38. $this->argumentResolver = $argument_resolver;
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function getTitle(Request $request, Route $route) {
  44. $route_title = NULL;
  45. // A dynamic title takes priority. Route::getDefault() returns NULL if the
  46. // named default is not set. By testing the value directly, we also avoid
  47. // trying to use empty values.
  48. if ($callback = $route->getDefault('_title_callback')) {
  49. $callable = $this->controllerResolver->getControllerFromDefinition($callback);
  50. $arguments = $this->argumentResolver->getArguments($request, $callable);
  51. $route_title = call_user_func_array($callable, $arguments);
  52. }
  53. elseif ($title = $route->getDefault('_title')) {
  54. $options = [];
  55. if ($context = $route->getDefault('_title_context')) {
  56. $options['context'] = $context;
  57. }
  58. $args = [];
  59. if (($raw_parameters = $request->attributes->get('_raw_variables'))) {
  60. foreach ($raw_parameters->all() as $key => $value) {
  61. $args['@' . $key] = $value;
  62. $args['%' . $key] = $value;
  63. }
  64. }
  65. if ($title_arguments = $route->getDefault('_title_arguments')) {
  66. $args = array_merge($args, (array) $title_arguments);
  67. }
  68. // Fall back to a static string from the route.
  69. $route_title = $this->t($title, $args, $options);
  70. }
  71. return $route_title;
  72. }
  73. }