Commit b7553795 authored by catch's avatar catch
Browse files

Issue #3334045 by yash.rode, joachim, quietone: Drupal\Core\Template\Attribute...

Issue #3334045 by yash.rode, joachim, quietone: Drupal\Core\Template\Attribute doesn't support adding attributes with array syntax if attribute name not already initialised

(cherry picked from commit ca19c94c)
parent b856b4bc
Loading
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -94,6 +94,13 @@ public function offsetGet($name): mixed {
    if (isset($this->storage[$name])) {
      return $this->storage[$name];
    }
    // The 'class' array key is expected to be itself an array, and therefore
    // can be accessed using array append syntax before it has been initialized.
    if ($name === 'class') {
      // Initialize the class attribute as an empty array if not set.
      $this->offsetSet('class', []);
      return $this->storage['class'];
    }
    return NULL;
  }

+6 −2
Original line number Diff line number Diff line
@@ -148,12 +148,16 @@ public function testRemoveAttribute(): void {
   * @covers ::addClass
   */
  public function testAddClasses(): void {
    // Add empty Attribute object with no classes.
    // Add a class with the array syntax without first initializing the 'class'
    // attribute.
    $attribute = new Attribute();
    $attribute['class'][] = 'test-class';
    $this->assertEquals(new AttributeArray('class', ['test-class']), $attribute['class']);

    $attribute = new Attribute();
    // Add no class on empty attribute.
    $attribute->addClass();
    $this->assertEmpty($attribute['class']);
    $this->assertEmpty($attribute['class']->value());

    // Test various permutations of adding values to empty Attribute objects.
    foreach ([NULL, FALSE, '', []] as $value) {