Skip to content
Snippets Groups Projects
Verified Commit 19ab1c98 authored by Lauri Timmanee's avatar Lauri Timmanee
Browse files

Issue #3403331 by pdureau, Utkarsh_33: Prevent TypeError when using create_attribute Twig function

parent 16fc21bb
No related branches found
No related tags found
No related merge requests found
......@@ -606,15 +606,18 @@ 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 = []) {
return new Attribute($attributes);
public function createAttribute(Attribute|array $attributes = []) {
if (\is_array($attributes)) {
return new Attribute($attributes);
}
return $attributes;
}
/**
......
......@@ -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;
......@@ -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.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment