diff --git a/core/lib/Drupal.php b/core/lib/Drupal.php index dcf962b..5d1588d 100644 --- a/core/lib/Drupal.php +++ b/core/lib/Drupal.php @@ -81,7 +81,7 @@ class Drupal { /** * The current system version. */ - const VERSION = '8.2.1-dev'; + const VERSION = '8.2.0-dev'; /** * Core API compatibility. diff --git a/core/lib/Drupal/Core/Template/TwigExtension.php b/core/lib/Drupal/Core/Template/TwigExtension.php index 3ebe01b..4524ea1 100644 --- a/core/lib/Drupal/Core/Template/TwigExtension.php +++ b/core/lib/Drupal/Core/Template/TwigExtension.php @@ -53,6 +53,13 @@ class TwigExtension extends \Twig_Extension { protected $dateFormatter; /** + * An empty Attribute object that can be cloned. + * + * @var \Drupal\Core\Template\Attribute + */ + protected $attribute; + + /** * Constructs \Drupal\Core\Template\TwigExtension. * * @param \Drupal\Core\Render\RendererInterface $renderer @@ -134,6 +141,7 @@ public function getFunctions() { new \Twig_SimpleFunction('attach_library', [$this, 'attachLibrary']), new \Twig_SimpleFunction('active_theme_path', [$this, 'getActiveThemePath']), new \Twig_SimpleFunction('active_theme', [$this, 'getActiveTheme']), + new \Twig_SimpleFunction('create_attribute', [$this, 'createAttribute']), ]; } @@ -594,4 +602,26 @@ public function safeJoin(\Twig_Environment $env, $value, $glue = '') { }, (array) $value)); } + /** + * Creates an Attribute object. + * + * @param array $attributes + * An associative array of key-value pairs to be converted to attributes. + * + * @return \Drupal\Core\Template\Attribute + * An attributes object that has the given attributes. + */ + public function createAttribute(array $attributes = []) { + if (!isset($this->attribute)) { + $this->attribute = new Attribute(); + } + $cloned_attributes = clone $this->attribute; + + foreach ($attributes as $name => $value) { + $cloned_attributes->offsetSet($name, $value); + } + + return $cloned_attributes; + } + } diff --git a/core/tests/Drupal/Tests/Core/Template/TwigExtensionTest.php b/core/tests/Drupal/Tests/Core/Template/TwigExtensionTest.php index ea93703..a0676d8 100644 --- a/core/tests/Drupal/Tests/Core/Template/TwigExtensionTest.php +++ b/core/tests/Drupal/Tests/Core/Template/TwigExtensionTest.php @@ -299,6 +299,26 @@ public function testRenderVarWithGeneratedLink() { $this->assertEquals('', $result); } + /** + * @covers ::createAttribute + */ + public function testCreateAttribute() { + $renderer = $this->prophesize(RendererInterface::class); + $extension = new TwigExtension($renderer->reveal()); + $loader = new StringLoader(); + $twig = new \Twig_Environment($loader); + $twig->addExtension($extension); + + $iterations = [ + ['class' => ['kittens'], 'data-toggle' =>['modal'], 'data-lang' => ['es']], + ['id' => ['puppies'], 'data-value' =>['foo'], 'data-lang' => ['en']], + [], + ]; + $result = $twig->render("{% for iteration in iterations %}{% endfor %}", ['iterations' => $iterations]); + $expected = '
'; + $this->assertEquals($expected, $result); + } + } class TwigExtensionTestString {