diff --git a/core/lib/Drupal/Core/Render/Element/Link.php b/core/lib/Drupal/Core/Render/Element/Link.php index 8f908d2..b5b3bd2 100644 --- a/core/lib/Drupal/Core/Render/Element/Link.php +++ b/core/lib/Drupal/Core/Render/Element/Link.php @@ -82,7 +82,8 @@ public static function preRenderLink($element) { $options = NestedArray::mergeDeep($element['#url']->getOptions(), $element['#options']); /** @var \Drupal\Core\Utility\LinkGenerator $link_generator */ $link_generator = \Drupal::service('link_generator'); - $generated_link = $link_generator->generate($element['#title'], $element['#url']->setOptions($options)); + $attributes = (!empty($element['#options']['attributes'])) ? $element['#options']['attributes'] : []; + $generated_link = $link_generator->generate($element['#title'], $element['#url']->setOptions($options), $attributes); $element['#markup'] = $generated_link->getGeneratedLink(); $generated_link->merge(BubbleableMetadata::createFromRenderArray($element)) ->applyTo($element); diff --git a/core/lib/Drupal/Core/Utility/LinkGenerator.php b/core/lib/Drupal/Core/Utility/LinkGenerator.php index 3163119..4c8e03b 100644 --- a/core/lib/Drupal/Core/Utility/LinkGenerator.php +++ b/core/lib/Drupal/Core/Utility/LinkGenerator.php @@ -81,12 +81,6 @@ public function generateFromLink(Link $link) { * @see system_page_attachments() */ public function generate($text, Url $url, $attributes = []) { - if (!($attributes instanceof Attribute)) { - $attributes = new Attribute($attributes); - } - // Include a placeholder for the href. - $attributes['href'] = ''; - // Performance: avoid Url::toString() needing to retrieve the URL generator // service from the container. $url->setUrlGenerator($this->urlGenerator); @@ -155,16 +149,17 @@ public function generate($text, Url $url, $attributes = []) { // Move attributes out of options since generateFromRoute() doesn't need // them. - foreach ($variables['options']['attributes'] as $attribute_name => $attribute_value) { - // Merge classes. - if ($attribute_name == 'class') { - $attributes->addClass($attribute_value); - } - elseif (!isset($attributes[$attribute_name])) { - // Set from options if not already provided on $attributes. - $attributes[$attribute_name] = $attribute_value; - } + if ($attributes instanceof Attribute) { + $attributes = $attributes->toArray(); } + if ($variables['options']['attributes']) { + $attributes = array_merge($variables['options']['attributes'], $attributes); + } + + // Include a placeholder for the href. + $attributes['href'] = ''; + $attributes = new Attribute($attributes); + unset($variables['options']['attributes']); $url->setOptions($variables['options']); diff --git a/core/modules/system/src/Tests/Theme/EngineTwigTest.php b/core/modules/system/src/Tests/Theme/EngineTwigTest.php index 283b41d..702eb16 100644 --- a/core/modules/system/src/Tests/Theme/EngineTwigTest.php +++ b/core/modules/system/src/Tests/Theme/EngineTwigTest.php @@ -86,6 +86,7 @@ public function testTwigLinkGenerator() { 'link via the linkgenerator: ' . $link_generator->generate('register', new Url('user.register', [], ['attributes' => ['foo' => 'bar', 'id' => 'kitten']])), 'link via the linkgenerator: ' . $link_generator->generate('register', new Url('user.register', [], ['attributes' => ['id' => 'kitten']])), 'link via the linkgenerator: ' . $link_generator->generate('register', new Url('user.register', [], ['attributes' => ['class' => ['llama', 'kitten', 'panda']]])), + 'link via the linkgenerator: ' . $link_generator->generate('register', new Url('user.register'), ['foo' => 'bar', 'id' => 'kitten']), ]; // Verify that link() has the ability to bubble cacheability metadata: @@ -96,6 +97,7 @@ public function testTwigLinkGenerator() { $content = $this->getRawContent(); $this->assertFalse(empty($content), 'Page content is not empty'); + $this->verbose($content); foreach ($expected as $string) { $this->assertRaw('
' . $string . '
'); }