Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. TwigExtension.php

function 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 759

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;
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal