Skip to content
Snippets Groups Projects
Verified Commit 2936a16a authored by Théodore Biadala's avatar Théodore Biadala
Browse files

Issue #3334045 by yash.rode, joachim, smustgrave, quietone:...

Issue #3334045 by yash.rode, joachim, smustgrave, quietone: Drupal\Core\Template\Attribute doesn't support adding attributes with array syntax if attribute name not already initialised
parent 43c52bdf
No related branches found
No related tags found
1 merge request!122353526426-warning-for-missing
Pipeline #449485 passed
......@@ -95,6 +95,13 @@ public function offsetGet($name) {
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'];
}
}
/**
......
......@@ -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) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment