diff --git a/core/lib/Drupal/Core/Template/TwigExtension.php b/core/lib/Drupal/Core/Template/TwigExtension.php index 7d67ff991c4cb6779ab7067dec151ffc95af4fa8..62ad45d377e45a7faa6f7f8d1b0ea01ccd34a47b 100644 --- a/core/lib/Drupal/Core/Template/TwigExtension.php +++ b/core/lib/Drupal/Core/Template/TwigExtension.php @@ -284,10 +284,7 @@ public function getLink($text, $url, $attributes = []) { if ($attributes instanceof Attribute) { $attributes = $attributes->toArray(); } - if ($existing_attributes = $url->getOption('attributes')) { - $attributes = array_merge($existing_attributes, $attributes); - } - $url->setOption('attributes', $attributes); + $url->mergeOptions(['attributes' => $attributes]); } // The text has been processed by twig already, convert it to a safe object // for the render system. diff --git a/core/tests/Drupal/Tests/Core/Template/TwigExtensionTest.php b/core/tests/Drupal/Tests/Core/Template/TwigExtensionTest.php index 396b2f9765ff9ce4ed5e68f936b9959e08e1c3a8..fe9f6e63cd10602e9f6a930c1f1727d59ce26d83 100644 --- a/core/tests/Drupal/Tests/Core/Template/TwigExtensionTest.php +++ b/core/tests/Drupal/Tests/Core/Template/TwigExtensionTest.php @@ -8,6 +8,7 @@ use Drupal\Core\Template\Loader\StringLoader; use Drupal\Core\Template\TwigEnvironment; use Drupal\Core\Template\TwigExtension; +use Drupal\Core\Url; use Drupal\Tests\UnitTestCase; /** @@ -345,6 +346,17 @@ public function testCreateAttribute() { $this->assertEquals($expected, $result); } + /** + * @covers ::getLink + */ + public function testLinkWithOverriddenAttributes() { + $url = Url::fromRoute('', [], ['attributes' => ['class' => ['foo']]]); + + $build = $this->systemUnderTest->getLink('test', $url, ['class' => ['bar']]); + + $this->assertEquals(['foo', 'bar'], $build['#url']->getOption('attributes')['class']); + } + } class TwigExtensionTestString {