summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Pott2015-07-16 22:13:26 (GMT)
committerAlex Pott2015-07-16 22:13:26 (GMT)
commitdf44c00a6c0256b0d592385bc264f10037b15655 (patch)
tree4e7def5075c0c0ffb22f9381a832a476f28c5c76
parent1aca0e483aa6540bcdb6df14e3a4d7925b85b372 (diff)
Issue #2506151 by joelpittet, Cottser, lauriii, Lukas von Blarer, tim.plunkett: Make the Twig extension link() accept Attribute objects
-rw-r--r--core/lib/Drupal/Core/Template/Attribute.php18
-rw-r--r--core/lib/Drupal/Core/Template/TwigExtension.php9
-rw-r--r--core/modules/system/src/Tests/Theme/EngineTwigTest.php1
-rw-r--r--core/modules/system/tests/modules/twig_theme_test/src/TwigThemeTestController.php8
-rw-r--r--core/modules/system/tests/modules/twig_theme_test/templates/twig_theme_test.link_generator.html.twig1
-rw-r--r--core/modules/system/tests/modules/twig_theme_test/twig_theme_test.module6
6 files changed, 36 insertions, 7 deletions
diff --git a/core/lib/Drupal/Core/Template/Attribute.php b/core/lib/Drupal/Core/Template/Attribute.php
index b8c0590..ff01040 100644
--- a/core/lib/Drupal/Core/Template/Attribute.php
+++ b/core/lib/Drupal/Core/Template/Attribute.php
@@ -43,10 +43,11 @@ use Drupal\Component\Utility\SafeStringInterface;
* htmlspecialchars() and the entire attribute string is marked safe for output.
*/
class Attribute implements \ArrayAccess, \IteratorAggregate, SafeStringInterface {
+
/**
* Stores the attribute data.
*
- * @var array
+ * @var \Drupal\Core\Template\AttributeValueBase[]
*/
protected $storage = array();
@@ -262,6 +263,21 @@ class Attribute implements \ArrayAccess, \IteratorAggregate, SafeStringInterface
}
/**
+ * Returns all storage elements as an array.
+ *
+ * @return array
+ * An associative array of attributes.
+ */
+ public function toArray() {
+ $return = [];
+ 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 52a0b59..482c888 100644
--- a/core/lib/Drupal/Core/Template/TwigExtension.php
+++ b/core/lib/Drupal/Core/Template/TwigExtension.php
@@ -260,17 +260,20 @@ class TwigExtension extends \Twig_Extension {
* 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 optional array of link attributes.
+ * @param array|\Drupal\Core\Template\Attribute $attributes
+ * An optional array or Attribute object 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 @@ class EngineTwigTest extends WebTestBase {
'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..dce85a3 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;
/**
@@ -53,11 +54,14 @@ class TwigThemeTestController {
* Renders for testing link_generator functions in a Twig template.
*/
public function linkGeneratorRender() {
- return array(
+ return [
'#theme' => 'twig_theme_test_link_generator',
'#test_url' => new Url('user.register', [], ['absolute' => TRUE]),
'#test_url_attribute' => new Url('user.register', [], ['attributes' => ['foo' => 'bar']]),
- );
+ // Explicitly creating an Attribute object to avoid false positives when
+ // testing Attribute object merging with the twig link() function.
+ '#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 @@
<div>link via the linkgenerator: {{ link('register', test_url, {'foo': 'bar'}) }}</div>
<div>link via the linkgenerator: {{ link('register', test_url_attribute, {'id': 'kitten'}) }}</div>
<div>link via the linkgenerator: {{ link('register', 'route:user.register', {'id': 'kitten'}) }}</div>
+<div>link via the linkgenerator: {{ link('register', 'route:user.register', attributes) }}</div>
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(