function TwigExtension::setAttribute

Same name and namespace in other branches
  1. 10 core/lib/Drupal/Core/Template/TwigExtension.php \Drupal\Core\Template\TwigExtension::setAttribute()

Sets an attribute on a given element.

Assumes the element is an array.

Parameters

array $element: A render element.

string $name: The attribute name.

mixed $value: (optional) The attribute value.

Return value

array The element with the given sanitized attribute's value.

File

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

Class

TwigExtension
A class providing Drupal Twig extensions.

Namespace

Drupal\Core\Template

Code

public function setAttribute(array $element, string $name, mixed $value = NULL) : array {
    $element['#attributes'] = AttributeHelper::mergeCollections($element['#attributes'] ?? [], new Attribute([
        $name => $value,
    ]));
    // 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.