Issue #2506151 by joelpittet, Cottser, lauriii, Lukas von Blarer, tim.plunkett: Make the Twig extension link() accept Attribute objects

8.0.x
Alex Pott 2015-07-16 23:13:26 +01:00
parent 1aca0e483a
commit df44c00a6c
6 changed files with 36 additions and 7 deletions

View File

@ -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();
@ -261,6 +262,21 @@ class Attribute implements \ArrayAccess, \IteratorAggregate, SafeStringInterface
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.
*/

View File

@ -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);
}

View File

@ -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:

View File

@ -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']]),
];
}
/**

View File

@ -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>

View File

@ -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(