diff --git a/core/lib/Drupal/Core/Template/Attribute.php b/core/lib/Drupal/Core/Template/Attribute.php index cebe699..5926024 100644 --- a/core/lib/Drupal/Core/Template/Attribute.php +++ b/core/lib/Drupal/Core/Template/Attribute.php @@ -189,6 +189,18 @@ public function removeClass() { } /** + * Checks if the class array has the given CSS class. + * + * @param string $class + * CSS class to check for. + * + * @return bool + */ + public function hasClass($class) { + return in_array($class, $this->storage['class']->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..49da086 100644 --- a/core/tests/Drupal/Tests/Core/Template/AttributeTest.php +++ b/core/tests/Drupal/Tests/Core/Template/AttributeTest.php @@ -141,6 +141,21 @@ public function testRemoveClasses() { } /** + * Tests checking for class names with the Attribute method. + * @covers ::hasClass() + */ + public function testHasClass() { + // Add a class to check for. + $classes = array('we-totally-have-this-class'); + $attribute = new Attribute(array('class' => $classes)); + + // Check that this class exists. + $this->assertTrue($attribute->hasClass('we-totally-have-this-class')); + // Check that this class does not exist. + $this->assertFalse($attribute->hasClass('a-class-nowhere-to-be-found')); + } + + /** * Tests removing class attributes with the Attribute helper methods. * @covers ::removeClass() * @covers ::addClass()