Verified Commit 768d0c77 authored by Lauri Timmanee's avatar Lauri Timmanee
Browse files

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

(cherry picked from commit 19ab1c98)
parent 5aacbb6e
Loading
Loading
Loading
Loading
Loading
+8 −5
Original line number Diff line number Diff line
@@ -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.
+3 −1
Original line number Diff line number Diff line
@@ -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.