Loading core/lib/Drupal/Core/Template/TwigExtension.php +8 −5 Original line number Diff line number Diff line Loading @@ -606,16 +606,19 @@ public function safeJoin(Environment $env, $value, $glue = '') { /** * Creates an Attribute object. * * @param array $attributes * (optional) An associative array of key-value pairs to be converted to * HTML attributes. * @param Attribute|array $attributes * (optional) An existing attribute object or an associative array of * key-value pairs to be converted to HTML attributes. * * @return \Drupal\Core\Template\Attribute * An attributes object that has the given attributes. */ public function createAttribute(array $attributes = []) { public function createAttribute(Attribute|array $attributes = []) { if (\is_array($attributes)) { return new Attribute($attributes); } return $attributes; } /** * Removes child elements from a copy of the original array. Loading core/tests/Drupal/Tests/Core/Template/TwigExtensionTest.php +3 −1 Original line number Diff line number Diff line Loading @@ -10,6 +10,7 @@ use Drupal\Core\Render\Markup; use Drupal\Core\Render\RenderableInterface; use Drupal\Core\StringTranslation\TranslatableMarkup; use Drupal\Core\Template\Attribute; use Drupal\Core\Template\Loader\StringLoader; use Drupal\Core\Template\TwigEnvironment; use Drupal\Core\Template\TwigExtension; Loading Loading @@ -404,9 +405,10 @@ public function testCreateAttribute() { ['class' => ['kittens'], 'data-toggle' => 'modal', 'data-lang' => 'es'], ['id' => 'puppies', 'data-value' => 'foo', 'data-lang' => 'en'], [], new Attribute(), ]; $result = $twig->render($name, ['iterations' => $iterations]); $expected = '<div class="kittens" data-toggle="modal" data-lang="es"></div><div id="puppies" data-value="foo" data-lang="en"></div><div></div>'; $expected = '<div class="kittens" data-toggle="modal" data-lang="es"></div><div id="puppies" data-value="foo" data-lang="en"></div><div></div><div></div>'; $this->assertEquals($expected, $result); // Test default creation of empty attribute object and using its method. Loading Loading
core/lib/Drupal/Core/Template/TwigExtension.php +8 −5 Original line number Diff line number Diff line Loading @@ -606,16 +606,19 @@ public function safeJoin(Environment $env, $value, $glue = '') { /** * Creates an Attribute object. * * @param array $attributes * (optional) An associative array of key-value pairs to be converted to * HTML attributes. * @param Attribute|array $attributes * (optional) An existing attribute object or an associative array of * key-value pairs to be converted to HTML attributes. * * @return \Drupal\Core\Template\Attribute * An attributes object that has the given attributes. */ public function createAttribute(array $attributes = []) { public function createAttribute(Attribute|array $attributes = []) { if (\is_array($attributes)) { return new Attribute($attributes); } return $attributes; } /** * Removes child elements from a copy of the original array. Loading
core/tests/Drupal/Tests/Core/Template/TwigExtensionTest.php +3 −1 Original line number Diff line number Diff line Loading @@ -10,6 +10,7 @@ use Drupal\Core\Render\Markup; use Drupal\Core\Render\RenderableInterface; use Drupal\Core\StringTranslation\TranslatableMarkup; use Drupal\Core\Template\Attribute; use Drupal\Core\Template\Loader\StringLoader; use Drupal\Core\Template\TwigEnvironment; use Drupal\Core\Template\TwigExtension; Loading Loading @@ -404,9 +405,10 @@ public function testCreateAttribute() { ['class' => ['kittens'], 'data-toggle' => 'modal', 'data-lang' => 'es'], ['id' => 'puppies', 'data-value' => 'foo', 'data-lang' => 'en'], [], new Attribute(), ]; $result = $twig->render($name, ['iterations' => $iterations]); $expected = '<div class="kittens" data-toggle="modal" data-lang="es"></div><div id="puppies" data-value="foo" data-lang="en"></div><div></div>'; $expected = '<div class="kittens" data-toggle="modal" data-lang="es"></div><div id="puppies" data-value="foo" data-lang="en"></div><div></div><div></div>'; $this->assertEquals($expected, $result); // Test default creation of empty attribute object and using its method. Loading