diff --git a/core/lib/Drupal/Core/Template/Attribute.php b/core/lib/Drupal/Core/Template/Attribute.php index b8c0590..9798b52 100644 --- a/core/lib/Drupal/Core/Template/Attribute.php +++ b/core/lib/Drupal/Core/Template/Attribute.php @@ -262,6 +262,19 @@ public function __toString() { } /** + * Returns all storage elements as an array. + */ + public function toArray() { + $return = []; + /** @var \Drupal\Core\Template\AttributeValueBase $value */ + foreach ($this->storage as $name => $value) { + $return[$name] = $value->value(); + } + + return $return; + } + + /** * Implements the magic __clone() method. */ public function __clone() { diff --git a/core/lib/Drupal/Core/Template/TwigExtension.php b/core/lib/Drupal/Core/Template/TwigExtension.php index b3bc425..2c1db98 100644 --- a/core/lib/Drupal/Core/Template/TwigExtension.php +++ b/core/lib/Drupal/Core/Template/TwigExtension.php @@ -260,17 +260,20 @@ 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 + * @param array|Attribute $attributes * An optional array of link attributes. * * @return array * A render array representing a link to the given URL. */ - public function getLink($text, $url, array $attributes = []) { + public function getLink($text, $url, $attributes = []) { if (!$url instanceof Url) { $url = Url::fromUri($url); } if ($attributes) { + if ($attributes instanceOf Attribute) { + $attributes = $attributes->toArray(); + } if ($existing_attributes = $url->getOption('attributes')) { $attributes = array_merge($existing_attributes, $attributes); } diff --git a/core/modules/system/src/Tests/Theme/EngineTwigTest.php b/core/modules/system/src/Tests/Theme/EngineTwigTest.php index 86b49e8..59af014 100644 --- a/core/modules/system/src/Tests/Theme/EngineTwigTest.php +++ b/core/modules/system/src/Tests/Theme/EngineTwigTest.php @@ -85,6 +85,7 @@ public function testTwigLinkGenerator() { 'link via the linkgenerator: ' . $link_generator->generate('register', new Url('user.register', [], ['absolute' => TRUE, 'attributes' => ['foo' => 'bar']])), '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']]])), ]; // Verify that link() has the ability to bubble cacheability metadata: diff --git a/core/modules/system/tests/modules/twig_theme_test/src/TwigThemeTestController.php b/core/modules/system/tests/modules/twig_theme_test/src/TwigThemeTestController.php index d49ebba..b6c4149 100644 --- a/core/modules/system/tests/modules/twig_theme_test/src/TwigThemeTestController.php +++ b/core/modules/system/tests/modules/twig_theme_test/src/TwigThemeTestController.php @@ -7,6 +7,7 @@ namespace Drupal\twig_theme_test; +use Drupal\Core\Template\Attribute; use Drupal\Core\Url; /** @@ -57,6 +58,7 @@ public function linkGeneratorRender() { '#theme' => 'twig_theme_test_link_generator', '#test_url' => new Url('user.register', [], ['absolute' => TRUE]), '#test_url_attribute' => new Url('user.register', [], ['attributes' => ['foo' => 'bar']]), + '#attributes' => new Attribute(['class' => ['llama', 'kitten', 'panda']]), ); } 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 3fb846f..8925705 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 @@ -2,3 +2,4 @@
link via the linkgenerator: {{ link('register', test_url, {'foo': 'bar'}) }}
link via the linkgenerator: {{ link('register', test_url_attribute, {'id': 'kitten'}) }}
link via the linkgenerator: {{ link('register', 'route:user.register', {'id': 'kitten'}) }}
+
link via the linkgenerator: {{ link('register', 'route:user.register', attributes) }}
diff --git a/core/modules/system/tests/modules/twig_theme_test/twig_theme_test.module b/core/modules/system/tests/modules/twig_theme_test/twig_theme_test.module index d5e098c..52c0ef4 100644 --- a/core/modules/system/tests/modules/twig_theme_test/twig_theme_test.module +++ b/core/modules/system/tests/modules/twig_theme_test/twig_theme_test.module @@ -43,7 +43,11 @@ function twig_theme_test_theme($existing, $type, $theme, $path) { 'template' => 'twig_theme_test.url_generator', ); $items['twig_theme_test_link_generator'] = array( - 'variables' => array('test_url' => NULL, 'test_url_attribute' => NULL), + 'variables' => [ + 'test_url' => NULL, + 'test_url_attribute' => NULL, + 'attributes' => [], + ], 'template' => 'twig_theme_test.link_generator', ); $items['twig_theme_test_url_to_string'] = array(