Commit df44c00a authored by alexpott's avatar alexpott

Issue #2506151 by joelpittet, Cottser, lauriii, Lukas von Blarer,...

Issue #2506151 by joelpittet, Cottser, lauriii, Lukas von Blarer, tim.plunkett: Make the Twig extension link() accept Attribute objects
parent 1aca0e48
......@@ -43,10 +43,11 @@
* 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();
......@@ -261,6 +262,21 @@ public function __toString() {
return $return;
}
/**
* 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.
*/
......
......@@ -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
* 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);
}
......
......@@ -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:
......
......@@ -7,6 +7,7 @@
namespace Drupal\twig_theme_test;
use Drupal\Core\Template\Attribute;
use Drupal\Core\Url;
/**
......@@ -53,11 +54,14 @@ public function urlGeneratorRender() {
* 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']]),
];
}
/**
......
......@@ -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>
......@@ -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(
......
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