Unverified Commit 0c9b4fcd authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3081044 by shalini_jha, nlisgo, vivek panicker, smustgrave, alexpott,...

Issue #3081044 by shalini_jha, nlisgo, vivek panicker, smustgrave, alexpott, quietone: Allow default title arguments to be overridden
parent cc1f5a02
Loading
Loading
Loading
Loading
Loading
+3 −3
Changes for core/lib/Drupal/Core/Controller/TitleResolver.php: 3 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -64,6 +64,9 @@ public function getTitle(Request $request, Route $route) {
        $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)) {
@@ -72,9 +75,6 @@ public function getTitle(Request $request, Route $route) {
          }
        }
      }
      if ($title_arguments = $route->getDefault('_title_arguments')) {
        $args = array_merge($args, (array) $title_arguments);
      }

      // Fall back to a static string from the route.
      // phpcs:ignore Drupal.Semantics.FunctionT.NotLiteralString
+46 −0
Changes for core/tests/Drupal/Tests/Core/Controller/TitleResolverTest.php: 46 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -108,6 +108,52 @@ public function testStaticTitleWithParameter(): void {
    $this->assertEquals(new TranslatableMarkup('static title %test', ['@test' => 'value', '%test' => 'value', '@test2' => 'value2', '%test2' => 'value2'], [], $this->translationManager), $this->titleResolver->getTitle($request, $route));
  }

  /**
   * Tests a static title with and without overridden default arguments.
   *
   * @see \Drupal\Core\Controller\TitleResolver::getTitle()
   */
  public function testStaticTitleWithArguments(): void {
    // Set up the request with optional override variables.
    $request = new Request();
    $raw_variables = new InputBag(['test' => 'override value']);

    // Array of cases.
    $cases = [
      // Case 1: No override, uses default arguments.
      [
        'route_args' => ['_title' => 'static title @test', '_title_arguments' => ['@test' => 'value', '@test2' => 'value2']],
        'expected' => new TranslatableMarkup('static title @test', ['@test' => 'value', '@test2' => 'value2'], [], $this->translationManager),
        'override' => FALSE,
      ],
      [
        'route_args' => ['_title' => 'static title %test', '_title_arguments' => ['%test' => 'value', '%test2' => 'value2']],
        'expected' => new TranslatableMarkup('static title %test', ['%test' => 'value', '%test2' => 'value2'], [], $this->translationManager),
        'override' => FALSE,
      ],
      // Case 2: Override arguments.
      [
        'route_args' => ['_title' => 'static title @test @test2', '_title_arguments' => ['@test' => 'value', '@test2' => 'value2']],
        'expected' => new TranslatableMarkup('static title @test @test2', ['@test' => 'override value', '%test' => 'override value', '@test2' => 'value2'], [], $this->translationManager),
        'override' => TRUE,
      ],
      [
        'route_args' => ['_title' => 'static title %test %test2', '_title_arguments' => ['%test' => 'value', '%test2' => 'value2']],
        'expected' => new TranslatableMarkup('static title %test %test2', ['@test' => 'override value', '%test' => 'override value', '%test2' => 'value2'], [], $this->translationManager),
        'override' => TRUE,
      ],
    ];

    foreach ($cases as $case) {
      // Adjust the request based on whether we expect overrides.
      if ($case['override']) {
        $request->attributes->set('_raw_variables', $raw_variables);
      }
      $route = new Route('/test-route', $case['route_args']);
      $this->assertEquals($case['expected'], $this->titleResolver->getTitle($request, $route));
    }
  }

  /**
   * Tests a static title with a non-scalar value parameter.
   *