diff --git a/core/lib/Drupal/Core/Template/Attribute.php b/core/lib/Drupal/Core/Template/Attribute.php index d7dae0e..5333acb 100644 --- a/core/lib/Drupal/Core/Template/Attribute.php +++ b/core/lib/Drupal/Core/Template/Attribute.php @@ -161,6 +161,47 @@ public function addClass() { } /** + * Sets values for an attribute key. + * + * @param string $attribute + * Name of the attribute. + * @param string|array $value + * Value(s) to set for the given attribute key. + * + * @return $this + */ + public function setAttribute($attribute, $value) { + $this->offsetSet($attribute, $value); + + return $this; + } + + /** + * Removes an attribute from an Attribute object. + * + * @param string|array ... + * Attributes to remove from the attribute array. + * + * @return $this + */ + public function removeAttribute() { + $args = func_get_args(); + foreach ($args as $arg) { + // Support arrays or multiple arguments. + if (is_array($arg)) { + foreach ($arg as $value) { + unset($this->storage[$value]); + } + } + else { + unset($this->storage[$arg]); + } + } + + return $this; + } + + /** * Removes argument values from array of existing CSS classes. * * @param string|array ... diff --git a/core/lib/Drupal/Core/Template/AttributeArray.php b/core/lib/Drupal/Core/Template/AttributeArray.php index 61c968f..190c898 100644 --- a/core/lib/Drupal/Core/Template/AttributeArray.php +++ b/core/lib/Drupal/Core/Template/AttributeArray.php @@ -87,13 +87,6 @@ public function getIterator() { } /** - * Returns the whole array. - */ - public function value() { - return $this->value; - } - - /** * Exchange the array for another one. * * @see ArrayObject::exchangeArray diff --git a/core/lib/Drupal/Core/Template/AttributeValueBase.php b/core/lib/Drupal/Core/Template/AttributeValueBase.php index 197b199..ed4dfc3 100644 --- a/core/lib/Drupal/Core/Template/AttributeValueBase.php +++ b/core/lib/Drupal/Core/Template/AttributeValueBase.php @@ -62,6 +62,13 @@ public function render() { } /** + * Returns the raw value. + */ + public function value() { + return $this->value; + } + + /** * Implements the magic __toString() method. */ abstract function __toString(); diff --git a/core/tests/Drupal/Tests/Core/Template/AttributeTest.php b/core/tests/Drupal/Tests/Core/Template/AttributeTest.php index 0384da8..fcf8601 100644 --- a/core/tests/Drupal/Tests/Core/Template/AttributeTest.php +++ b/core/tests/Drupal/Tests/Core/Template/AttributeTest.php @@ -58,6 +58,79 @@ public function testRemove() { } /** + * Tests setting attributes. + * @covers ::setAttribute() + */ + public function testSetAttribute() { + $attribute = new Attribute(); + + // Test adding various attributes. + $attributes = ['alt', 'id', 'src', 'title', 'value']; + foreach ($attributes as $key) { + foreach (['kitten', ''] as $value) { + $attribute = new Attribute(); + $attribute->setAttribute($key, $value); + $this->assertEquals($value, $attribute[$key]); + } + } + + // Test adding array to class. + $attribute = new Attribute(); + $attribute->setAttribute('class', ['kitten', 'cat']); + $this->assertArrayEquals(['kitten', 'cat'], $attribute['class']->value()); + + // Test adding boolean attributes. + $attribute = new Attribute(); + $attribute['checked'] = TRUE; + $this->assertTrue($attribute['checked']->value()); + + // Test adding boolean attributes through the constructor. + $attribute = new Attribute(['selected' => TRUE, 'checked' => FALSE]); + $this->assertTrue($attribute['selected']->value()); + $this->assertFalse($attribute['checked']->value()); + } + + /** + * Tests removing attributes. + * @covers ::removeAttribute() + */ + public function testRemoveAttribute() { + $attributes = [ + 'alt' => 'Alternative text', + 'id' => 'bunny', + 'src' => 'zebra', + 'style' => 'color: pink;', + 'title' => 'kitten', + 'value' => 'ostrich', + ]; + $attribute = new Attribute($attributes); + + // Single value. + $attribute->removeAttribute('alt'); + $this->assertEmpty($attribute['alt']); + + // Multiple values. + $attribute->removeAttribute('id', 'src'); + $this->assertEmpty($attribute['id']); + $this->assertEmpty($attribute['src']); + + // Single value in array. + $attribute->removeAttribute(['style']); + $this->assertEmpty($attribute['style']); + + // Multiple values in array. + $attribute->removeAttribute(['title', 'value']); + $this->assertEmpty((string) $attribute); + + // Boolean value. + $attribute = new Attribute(); + $attribute->setAttribute('checked', TRUE); + $this->assertTrue($attribute['checked']->value()); + $attribute->removeAttribute('checked'); + $this->assertEmpty($attribute['checked']); + } + + /** * Tests adding class attributes with the AttributeArray helper method. * @covers ::addClass() */