function TwigExtension::addClass

Same name in other branches
  1. 11.x core/lib/Drupal/Core/Template/TwigExtension.php \Drupal\Core\Template\TwigExtension::addClass()

Adds a value into the class attributes of a given element.

Assumes element is an array.

Parameters

array $element: A render element.

string[]|string ...$classes: The class(es) to add to the element. Arguments can include string keys directly, or arrays of string keys.

Return value

array The element with the given class(es) in attributes.

File

core/lib/Drupal/Core/Template/TwigExtension.php, line 753

Class

TwigExtension
A class providing Drupal Twig extensions.

Namespace

Drupal\Core\Template

Code

public function addClass(array $element, ...$classes) : array {
    $attributes = new Attribute($element['#attributes'] ?? []);
    $attributes->addClass(...$classes);
    $element['#attributes'] = $attributes->toArray();
    // Make sure element gets rendered again.
    unset($element['#printed']);
    return $element;
}

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