diff --git a/core/lib/Drupal/Core/Template/Attribute.php b/core/lib/Drupal/Core/Template/Attribute.php index 48e2fa1..ffef99a 100644 --- a/core/lib/Drupal/Core/Template/Attribute.php +++ b/core/lib/Drupal/Core/Template/Attribute.php @@ -80,8 +80,18 @@ public function offsetSet($name, $value) { * An AttributeValueBase representation of the attribute's value. */ protected function createAttributeValue($name, $value) { - if (is_array($value)) { - $value = new AttributeArray($name, $value); + // If the value is already an AttributeValueBase object, return it + // straight away. + if ($value instanceOf AttributeValueBase) { + return $value; + } + // An array value or 'class' attribute name are forced to always be an + // AttributeArray value for consistency. + if (is_array($value) || $name == 'class') { + // Cast the value to an array if the value was passed in as a string. + // @todo Decide to fix all the broken instances of class as a string + // in core or cast them. + $value = new AttributeArray($name, (array) $value); } elseif (is_bool($value)) { $value = new AttributeBoolean($name, $value); @@ -107,6 +117,26 @@ public function offsetExists($name) { } /** + * Adds the argument values to the class element. + */ + public function addClass() { + $args = func_get_args(); + foreach ($args as $arg) { + $this->storage['class']->add($arg); + } + } + + /** + * Removes the argument values from the class element. + */ + public function removeClass() { + $args = func_get_args(); + foreach ($args as $arg) { + $this->storage['class']->remove($arg); + } + } + + /** * Implements the magic __toString() method. */ public function __toString() { diff --git a/core/lib/Drupal/Core/Template/AttributeArray.php b/core/lib/Drupal/Core/Template/AttributeArray.php index 95e0ef3..277a9bb 100644 --- a/core/lib/Drupal/Core/Template/AttributeArray.php +++ b/core/lib/Drupal/Core/Template/AttributeArray.php @@ -70,6 +70,44 @@ public function __toString() { } /** + * Adds the argument values by merging them on to the value array. + * + * @param mixed $value + * A value, or array of values, to add to the array. + * + * @return \Drupal\Core\Template\AttributeArray + * The attribute array with the arguments added. + */ + public function add($value) { + // Merge the values passed in from the value array. + // The argument is cast to an array to support comma separated single + // values or one or more array arguments. + $this->value = array_merge($this->value, (array) $value); + // Filter out any empty values. + $this->value = array_filter($this->value); + return $this; + } + + /** + * Removes the argument values from the value array. + * + * @param mixed $value + * A value, or array of values, to remove from the array. + * + * @return \Drupal\Core\Template\AttributeArray + * The attribute array with the arguments removed. + */ + public function remove($value) { + // Remove the values passed in from the value array. + // The argument is cast to an array to support comma separated single + // values or one or more array arguments. + $this->value = array_diff($this->value, (array) $value); + // Filter out any empty values. + $this->value = array_filter($this->value); + return $this; + } + + /** * Implements IteratorAggregate::getIterator(). */ public function getIterator() { diff --git a/core/tests/Drupal/Tests/Core/Template/AttributeTest.php b/core/tests/Drupal/Tests/Core/Template/AttributeTest.php index aee1dd7..b8fc41d 100644 --- a/core/tests/Drupal/Tests/Core/Template/AttributeTest.php +++ b/core/tests/Drupal/Tests/Core/Template/AttributeTest.php @@ -57,6 +57,72 @@ public function testRemove() { $this->assertFalse(isset($attribute['class'])); } + + /** + * Tests adding class attributes with the AttributeArray helper method. + */ + public function testAddClasses() { + $attribute = new Attribute(array('class' => array('example-class'))); + + // Add one class. + $attribute->addClass('aa'); + $this->assertEquals(new AttributeArray('class', array('example-class', 'aa')), $attribute['class']); + + // Add multiple classes. + $attribute->addClass('xx', 'yy'); + $this->assertEquals( + new AttributeArray( + 'class', + array('example-class', 'aa', 'xx', 'yy')), + $attribute['class'] + ); + + // Add an array of classes. + $attribute->addClass(array('red', 'green', 'blue')); + $this->assertEquals( + new AttributeArray( + 'class', + array('example-class', 'aa', 'xx', 'yy', 'red', 'green', 'blue') + ), + $attribute['class'] + ); + + } + + /** + * Tests removing class attributes with the AttributeArray helper method. + */ + public function testRemoveClasses() { + $attribute = new Attribute( + array( + 'class' => array('example-class', 'aa', 'xx', 'yy', 'red', 'green', 'blue'), + ) + ); + + // Remove one class. + $attribute->removeClass('example-class'); + $this->assertFalse(in_array('example-class', $attribute['class']->value())); + + // Remove multiple classes. + $attribute->removeClass('xx', 'yy'); + $this->assertFalse(in_array(array('xx', 'yy'), $attribute['class']->value())); + + // Remove an array of classes. + $attribute->removeClass(array('red', 'green', 'blue')); + $this->assertFalse(in_array(array('red', 'green', 'blue'), $attribute['class']->value())); + } + + /** + * Tests removing class attributes with the AttributeArray helper method. + */ + public function testChainAddRemoveClasses() { + $attribute = new Attribute( + array('class' => array('example-class', 'red', 'green', 'blue')) + ); + unset($attribute['class']); + $this->assertFalse(isset($attribute['class'])); + } + /** * Tests iterating on the values of the attribute. */