Commit ba068310 authored by alexpott's avatar alexpott
Browse files

Issue #2847611 by dawehner, webflo:...

Issue #2847611 by dawehner, webflo: \Drupal\Core\Template\TwigExtension::getLink overrides existing classes
parent 1ff29a38
...@@ -284,10 +284,7 @@ public function getLink($text, $url, $attributes = []) { ...@@ -284,10 +284,7 @@ public function getLink($text, $url, $attributes = []) {
if ($attributes instanceof Attribute) { if ($attributes instanceof Attribute) {
$attributes = $attributes->toArray(); $attributes = $attributes->toArray();
} }
if ($existing_attributes = $url->getOption('attributes')) { $url->mergeOptions(['attributes' => $attributes]);
$attributes = array_merge($existing_attributes, $attributes);
}
$url->setOption('attributes', $attributes);
} }
// The text has been processed by twig already, convert it to a safe object // The text has been processed by twig already, convert it to a safe object
// for the render system. // for the render system.
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
use Drupal\Core\Template\Loader\StringLoader; use Drupal\Core\Template\Loader\StringLoader;
use Drupal\Core\Template\TwigEnvironment; use Drupal\Core\Template\TwigEnvironment;
use Drupal\Core\Template\TwigExtension; use Drupal\Core\Template\TwigExtension;
use Drupal\Core\Url;
use Drupal\Tests\UnitTestCase; use Drupal\Tests\UnitTestCase;
/** /**
...@@ -345,6 +346,17 @@ public function testCreateAttribute() { ...@@ -345,6 +346,17 @@ public function testCreateAttribute() {
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
} }
/**
* @covers ::getLink
*/
public function testLinkWithOverriddenAttributes() {
$url = Url::fromRoute('<front>', [], ['attributes' => ['class' => ['foo']]]);
$build = $this->systemUnderTest->getLink('test', $url, ['class' => ['bar']]);
$this->assertEquals(['foo', 'bar'], $build['#url']->getOption('attributes')['class']);
}
} }
class TwigExtensionTestString { class TwigExtensionTestString {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment