diff --git a/core/lib/Drupal/Core/Template/TwigExtension.php b/core/lib/Drupal/Core/Template/TwigExtension.php index 632e42a..1b06f99 100644 --- a/core/lib/Drupal/Core/Template/TwigExtension.php +++ b/core/lib/Drupal/Core/Template/TwigExtension.php @@ -204,17 +204,24 @@ public function getUrlFromPath($path, $options = array()) { * The link text for the anchor tag as a translated string. * @param \Drupal\Core\Url|string $url * The URL object or string used for the link. + * @param array $attributes + * An array of link attributes * * @return string * An HTML string containing a link to the given url. */ - public function getLink($text, $url) { + public function getLink($text, $url, $attributes = array()) { if ($url instanceof Url) { + $existing_attributes = $url->getOption('attributes'); + if (!$existing_attributes) { + $existing_attributes = array(); + } + $url->setOption('attributes', array_merge($existing_attributes , $attributes)); return $this->linkGenerator->generate($text, $url); } else { // @todo Convert once https://www.drupal.org/node/2306901 is in - return _l($text, $url); + return _l($text, $url, array('attributes' => $attributes)); } } diff --git a/core/modules/system/src/Tests/Theme/EngineTwigTest.php b/core/modules/system/src/Tests/Theme/EngineTwigTest.php index acfd1d1..b31c3b7 100644 --- a/core/modules/system/src/Tests/Theme/EngineTwigTest.php +++ b/core/modules/system/src/Tests/Theme/EngineTwigTest.php @@ -70,10 +70,12 @@ public function testTwigUrlGenerator() { public function testTwigLinkGenerator() { $this->drupalGet('twig-theme-test/link-generator'); + /** @var \Drupal\Core\Utility\LinkGenerator $link_generator */ $link_generator = $this->container->get('link_generator'); $expected = [ 'link via the linkgenerator: ' . $link_generator->generate('register', new Url('user.register')), + 'link via the linkgenerator: ' . $link_generator->generate('register', new Url('user.register', [], ['attributes' => ['foo' => 'bar']])), ]; $content = $this->getRawContent(); diff --git a/core/modules/system/tests/modules/twig_theme_test/templates/twig_theme_test.link_generator.html.twig b/core/modules/system/tests/modules/twig_theme_test/templates/twig_theme_test.link_generator.html.twig index a365859..b5ecd23 100644 --- a/core/modules/system/tests/modules/twig_theme_test/templates/twig_theme_test.link_generator.html.twig +++ b/core/modules/system/tests/modules/twig_theme_test/templates/twig_theme_test.link_generator.html.twig @@ -1 +1,2 @@
link via the linkgenerator: {{ link("register", test_url) }}
+
link via the linkgenerator: {{ link("register", test_url, { 'foo' : 'bar' }) }}