diff --git a/core/lib/Drupal/Core/Template/Attribute.php b/core/lib/Drupal/Core/Template/Attribute.php index 48e2fa1..00b3142 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,65 @@ public function offsetExists($name) { } /** + * Adds the argument values by merging them on to the value array. + * + * @return \Drupal\Core\Template\Attribute + * The attribute with the class arguments added. + */ + public function addClass() { + $args = func_get_args(); + $classes = array(); + foreach ($args as $arg) { + // Merge the values passed in from the classes array. + // The argument is cast to an array to support comma separated single + // values or one or more array arguments. + $classes = array_merge($classes, (array) $arg); + } + // Merge if there are values, just add them otherwise. + if (isset($this->storage['class']) && $this->storage['class'] instanceOf AttributeArray) { + // Merge the values passed in from the class value array. + $classes = array_merge($this->storage['class']->value(), $classes); + // Filter out any empty values. + $classes = array_filter($classes); + $this->storage['class']->exchangeArray($classes); + } + else { + // Filter out any empty values. + $classes = array_filter($classes); + $this->offsetSet('class', $classes); + } + + return $this; + } + + /** + * Removes the argument values from the value array. + * + * @return \Drupal\Core\Template\Attribute + * The attribute with the class arguments removed. + */ + public function removeClass() { + // With no class attribute, there is no need to remove. + if (isset($this->storage['class']) && $this->storage['class'] instanceOf AttributeArray) { + $args = func_get_args(); + $classes = array(); + foreach ($args as $arg) { + // Merge the values passed in from the classes array. + // The argument is cast to an array to support comma separated single + // values or one or more array arguments. + $classes = array_merge($classes, (array) $arg); + } + + // Remove the values passed in from the value array. + $classes = array_diff($this->storage['class']->value(), $classes); + // Filter out any empty values. + $classes = array_filter($classes); + $this->storage['class']->exchangeArray($classes); + } + return $this; + } + + /** * 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..a204f4b 100644 --- a/core/lib/Drupal/Core/Template/AttributeArray.php +++ b/core/lib/Drupal/Core/Template/AttributeArray.php @@ -83,4 +83,21 @@ public function value() { return $this->value; } + /** + * Exchange the array for another one. + * + * @see ArrayObject::exchangeArray + * + * @param array $input + * The array input to replace the internal value. + * + * @return array + * The old array value. + */ + public function exchangeArray($input) { + $old = $this->value; + $this->value = $input; + return $old; + } + } diff --git a/core/tests/Drupal/Tests/Core/Template/AttributeTest.php b/core/tests/Drupal/Tests/Core/Template/AttributeTest.php index aee1dd7..c09d3e0 100644 --- a/core/tests/Drupal/Tests/Core/Template/AttributeTest.php +++ b/core/tests/Drupal/Tests/Core/Template/AttributeTest.php @@ -58,6 +58,70 @@ public function testRemove() { } /** + * Tests adding class attributes with the AttributeArray helper method. + * @covers ::addClass() + */ + public function testAddClasses() { + + // Add empty Attribute object with no classes. + $attribute = new Attribute(); + // Add one class on empty attribute. + $attribute->addClass('banana'); + $this->assertEquals(new AttributeArray('class', array('banana')), $attribute['class']); + + // Add one class. + $attribute->addClass('aa'); + $this->assertEquals(new AttributeArray('class', array('banana', 'aa')), $attribute['class']); + + // Add multiple classes. + $attribute->addClass('xx', 'yy'); + $this->assertEquals(new AttributeArray('class', array('banana', 'aa', 'xx', 'yy')), $attribute['class']); + + // Add an array of classes. + $attribute->addClass(array('red', 'green', 'blue')); + $this->assertEquals(new AttributeArray('class', array('banana', 'aa', 'xx', 'yy', 'red', 'green', 'blue')), $attribute['class']); + } + + /** + * Tests removing class attributes with the AttributeArray helper method. + * @covers ::removeClass() + */ + public function testRemoveClasses() { + $classes = array('example-class', 'aa', 'xx', 'yy', 'red', 'green', 'blue'); + $attribute = new Attribute(array('class' => $classes)); + + // 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. + * @covers ::removeClass() + * @covers ::addClass() + */ + public function testChainAddRemoveClasses() { + $attribute = new Attribute( + array('class' => array('example-class', 'red', 'green', 'blue')) + ); + + $attribute + ->removeClass(array('red', 'green', 'pink')) + ->addClass(array('apple', 'lime', 'grapefruit')) + ->addClass(array('banana')); + $expected = array('example-class', 'blue', 'apple', 'lime', 'grapefruit', 'banana'); + $this->assertArrayEquals($expected, $attribute['class']->value(), 'Attributes chained'); + } + + /** * Tests iterating on the values of the attribute. */ public function testIterate() {