diff --git a/core/lib/Drupal/Core/Template/Attribute.php b/core/lib/Drupal/Core/Template/Attribute.php index cebe699..2eb520d 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/AttributeBoolean.php b/core/lib/Drupal/Core/Template/AttributeBoolean.php index 4e9ea67..d3d4a45 100644 --- a/core/lib/Drupal/Core/Template/AttributeBoolean.php +++ b/core/lib/Drupal/Core/Template/AttributeBoolean.php @@ -39,6 +39,13 @@ public function render() { } /** + * Returns the boolean value. + */ + public function value() { + return $this->value; + } + + /** * Implements the magic __toString() method. */ public function __toString() { diff --git a/core/tests/Drupal/Tests/Core/Template/AttributeTest.php b/core/tests/Drupal/Tests/Core/Template/AttributeTest.php index b04eb51..784d715 100644 --- a/core/tests/Drupal/Tests/Core/Template/AttributeTest.php +++ b/core/tests/Drupal/Tests/Core/Template/AttributeTest.php @@ -58,6 +58,74 @@ public function testRemove() { } /** + * Tests setting attributes. + * @covers ::setAttribute() + */ + public function testSetAttribute() { + $attribute = new Attribute(); + + // Test adding various attributes. + $attributes = array('alt', 'id', 'src', 'title', 'value'); + foreach ($attributes as $key) { + foreach (array('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', array('kitten', 'cat')); + $this->assertArrayEquals(array('kitten', 'cat'), $attribute['class']->value()); + + // Test adding boolean attributes. + $attribute = new Attribute(); + $attribute['checked'] = TRUE; + $this->assertTrue($attribute['checked']->value()); + } + + /** + * Tests removing attributes. + * @covers ::removeAttribute() + */ + public function testRemoveAttribute() { + $attributes = array( + '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() */