function Attribute::removeClass

Same name in other branches
  1. 9 core/lib/Drupal/Core/Template/Attribute.php \Drupal\Core\Template\Attribute::removeClass()
  2. 8.9.x core/lib/Drupal/Core/Template/Attribute.php \Drupal\Core\Template\Attribute::removeClass()
  3. 11.x core/lib/Drupal/Core/Template/Attribute.php \Drupal\Core\Template\Attribute::removeClass()

Removes argument values from array of existing CSS classes.

Parameters

string|array ...: CSS classes to remove from the class attribute array.

Return value

$this

File

core/lib/Drupal/Core/Template/Attribute.php, line 266

Class

Attribute
Collects, sanitizes, and renders HTML attributes.

Namespace

Drupal\Core\Template

Code

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 = [];
        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) $arg;
        }
        $classes = array_merge(...$classes);
        // Remove the values passed in from the value array. Use array_values() to
        // ensure that the array index remains sequential.
        $classes = array_values(array_diff($this->storage['class']
            ->value(), $classes));
        $this->storage['class']
            ->exchangeArray($classes);
    }
    return $this;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.