diff -u b/core/lib/Drupal/Core/Template/Attribute.php b/core/lib/Drupal/Core/Template/Attribute.php --- b/core/lib/Drupal/Core/Template/Attribute.php +++ b/core/lib/Drupal/Core/Template/Attribute.php @@ -117,6 +117,26 @@ } /** + * 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 -u b/core/lib/Drupal/Core/Template/AttributeArray.php b/core/lib/Drupal/Core/Template/AttributeArray.php --- b/core/lib/Drupal/Core/Template/AttributeArray.php +++ b/core/lib/Drupal/Core/Template/AttributeArray.php @@ -72,18 +72,17 @@ /** * Adds the argument values by merging them on to the value array. * + * @param string $value + * A value to add to the array. + * * @return \Drupal\Core\Template\AttributeArray * The attribute array with the arguments added. */ - public function add() { - $args = func_get_args(); - foreach ($args as $arg) { - // 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) $arg); - } - + 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; @@ -92,18 +91,17 @@ /** * Removes the argument values from the value array. * + * @param string $value + * A value to remove to the array. + * * @return \Drupal\Core\Template\AttributeArray * The attribute array with the arguments removed. */ - public function remove() { - $args = func_get_args(); - foreach ($args as $arg) { - // 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) $arg); - } - + 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; diff -u b/core/tests/Drupal/Tests/Core/Template/AttributeTest.php b/core/tests/Drupal/Tests/Core/Template/AttributeTest.php --- b/core/tests/Drupal/Tests/Core/Template/AttributeTest.php +++ b/core/tests/Drupal/Tests/Core/Template/AttributeTest.php @@ -65,11 +65,11 @@ $attribute = new Attribute(array('class' => array('example-class'))); // Add one class. - $attribute['class']->add('aa'); + $attribute->addClass('aa'); $this->assertEquals(new AttributeArray('class', array('example-class', 'aa')), $attribute['class']); // Add multiple classes. - $attribute['class']->add('xx', 'yy'); + $attribute->addClass('xx', 'yy'); $this->assertEquals( new AttributeArray( 'class', @@ -78,7 +78,7 @@ ); // Add an array of classes. - $attribute['class']->add(array('red', 'green', 'blue')); + $attribute->addClass(array('red', 'green', 'blue')); $this->assertEquals( new AttributeArray( 'class', @@ -100,15 +100,15 @@ ); // Remove one class. - $attribute['class']->remove('example-class'); + $attribute->removeClass('example-class'); $this->assertFalse(in_array('example-class', $attribute['class']->value())); // Remove multiple classes. - $attribute['class']->remove('xx', 'yy'); + $attribute->removeClass('xx', 'yy'); $this->assertFalse(in_array(array('xx', 'yy'), $attribute['class']->value())); // Remove an array of classes. - $attribute['class']->remove(array('red', 'green', 'blue')); + $attribute->removeClass(array('red', 'green', 'blue')); $this->assertFalse(in_array(array('red', 'green', 'blue'), $attribute['class']->value())); }