From 76b1a8baf49e04bd4c0af7cc49123e8194417117 Mon Sep 17 00:00:00 2001 From: Lee Rowlands <lee.rowlands@previousnext.com.au> Date: Tue, 21 Sep 2021 15:00:45 +1000 Subject: [PATCH] Issue #3236789 by alexpott: Drupal\Core\Controller\TitleResolver::getTitle() can result in PHP 8.1 deprecation notices due to NULL being set as placeholder values --- .../Drupal/Core/Controller/TitleResolver.php | 4 ++-- .../Core/Controller/TitleResolverTest.php | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/core/lib/Drupal/Core/Controller/TitleResolver.php b/core/lib/Drupal/Core/Controller/TitleResolver.php index 85fce7732c10..2cf06eca7e4a 100644 --- a/core/lib/Drupal/Core/Controller/TitleResolver.php +++ b/core/lib/Drupal/Core/Controller/TitleResolver.php @@ -65,8 +65,8 @@ public function getTitle(Request $request, Route $route) { $args = []; if (($raw_parameters = $request->attributes->get('_raw_variables'))) { foreach ($raw_parameters->all() as $key => $value) { - $args['@' . $key] = $value; - $args['%' . $key] = $value; + $args['@' . $key] = $value ?? ''; + $args['%' . $key] = $value ?? ''; } } if ($title_arguments = $route->getDefault('_title_arguments')) { diff --git a/core/tests/Drupal/Tests/Core/Controller/TitleResolverTest.php b/core/tests/Drupal/Tests/Core/Controller/TitleResolverTest.php index 27a654559a3d..3c26a0565127 100644 --- a/core/tests/Drupal/Tests/Core/Controller/TitleResolverTest.php +++ b/core/tests/Drupal/Tests/Core/Controller/TitleResolverTest.php @@ -102,6 +102,24 @@ public function providerTestStaticTitleWithParameter() { ]; } + /** + * Tests a static title with a NULL value parameter. + * + * @see \Drupal\Core\Controller\TitleResolver::getTitle() + */ + public function testStaticTitleWithNullValueParameter() { + $raw_variables = new ParameterBag(['test' => NULL, 'test2' => 'value']); + $request = new Request(); + $request->attributes->set('_raw_variables', $raw_variables); + + $route = new Route('/test-route', ['_title' => 'static title %test @test']); + $translatable_markup = $this->titleResolver->getTitle($request, $route); + $this->assertSame('', $translatable_markup->getArguments()['@test']); + $this->assertSame('', $translatable_markup->getArguments()['%test']); + $this->assertSame('value', $translatable_markup->getArguments()['@test2']); + $this->assertSame('value', $translatable_markup->getArguments()['%test2']); + } + /** * Tests a dynamic title. * -- GitLab