Same name and namespace in other branches
  1. 8.9.x core/lib/Drupal/Core/Controller/TitleResolver.php \Drupal\Core\Controller\TitleResolver
  2. 9 core/lib/Drupal/Core/Controller/TitleResolver.php \Drupal\Core\Controller\TitleResolver

Provides the default implementation of the title resolver interface.

Hierarchy

Expanded class hierarchy of TitleResolver

1 file declares its use of TitleResolver
TitleResolverTest.php in core/tests/Drupal/Tests/Core/Controller/TitleResolverTest.php
1 string reference to 'TitleResolver'
core.services.yml in core/core.services.yml
core/core.services.yml
1 service uses TitleResolver
title_resolver in core/core.services.yml
Drupal\Core\Controller\TitleResolver

File

core/lib/Drupal/Core/Controller/TitleResolver.php, line 14

Namespace

Drupal\Core\Controller
View source
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 ($raw_parameters = $request->attributes
        ->get('_raw_variables')) {
        foreach ($raw_parameters
          ->all() as $key => $value) {
          if (is_scalar($value)) {
            $args['@' . $key] = $value;
            $args['%' . $key] = $value;
          }
        }
      }
      if ($title_arguments = $route
        ->getDefault('_title_arguments')) {
        $args = array_merge($args, (array) $title_arguments);
      }

      // Fall back to a static string from the route.
      $route_title = $this
        ->t($title, $args, $options);
    }
    return $route_title;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
StringTranslationTrait::$stringTranslation protected property The string translation service. 3
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 1
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
TitleResolver::$argumentResolver protected property The argument resolver.
TitleResolver::$controllerResolver protected property The controller resolver.
TitleResolver::getTitle public function
TitleResolver::__construct public function Constructs a TitleResolver instance.