diff --git a/core/lib/Drupal/Core/Template/Attribute.php b/core/lib/Drupal/Core/Template/Attribute.php index 63f21c2d1626cce1e41e9233c84907bdab261b20..7422e26f463dc42b864dd937ea728e68ab41fd7e 100644 --- a/core/lib/Drupal/Core/Template/Attribute.php +++ b/core/lib/Drupal/Core/Template/Attribute.php @@ -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; } diff --git a/core/tests/Drupal/Tests/Core/Template/AttributeTest.php b/core/tests/Drupal/Tests/Core/Template/AttributeTest.php index f76bc92c757649e9116694148c7c2a120e283c6a..46ac4daf36dfea5e86614f1cd6561f0ce8a69d07 100644 --- a/core/tests/Drupal/Tests/Core/Template/AttributeTest.php +++ b/core/tests/Drupal/Tests/Core/Template/AttributeTest.php @@ -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) {