function Element::setAttributes

Same name in other branches
  1. 8.9.x core/lib/Drupal/Core/Render/Element.php \Drupal\Core\Render\Element::setAttributes()
  2. 10 core/lib/Drupal/Core/Render/Element.php \Drupal\Core\Render\Element::setAttributes()
  3. 11.x core/lib/Drupal/Core/Render/Element.php \Drupal\Core\Render\Element::setAttributes()

Sets HTML attributes based on element properties.

Parameters

array $element: The renderable element to process. Passed by reference.

array $map: An associative array whose keys are element property names and whose values are the HTML attribute names to set on the corresponding property; e.g., array('#property_name' => 'attribute_name'). If both names are identical except for the leading '#', then an attribute name value is sufficient and no property name needs to be specified.

25 calls to Element::setAttributes()
Button::preRenderButton in core/lib/Drupal/Core/Render/Element/Button.php
Prepares a #type 'button' render element for input.html.twig.
Checkbox::preRenderCheckbox in core/lib/Drupal/Core/Render/Element/Checkbox.php
Prepares a #type 'checkbox' render element for input.html.twig.
Color::preRenderColor in core/lib/Drupal/Core/Render/Element/Color.php
Prepares a #type 'color' render element for input.html.twig.
Date::preRenderDate in core/lib/Drupal/Core/Render/Element/Date.php
Adds form-specific attributes to a 'date' #type element.
Details::preRenderDetails in core/lib/Drupal/Core/Render/Element/Details.php
Adds form element theming to details.

... See full list

File

core/lib/Drupal/Core/Render/Element.php, line 174

Class

Element
Provides helper methods for Drupal render elements.

Namespace

Drupal\Core\Render

Code

public static function setAttributes(array &$element, array $map) {
    foreach ($map as $property => $attribute) {
        // If the key is numeric, the attribute name needs to be taken over.
        if (is_int($property)) {
            $property = '#' . $attribute;
        }
        // Do not overwrite already existing attributes.
        if (isset($element[$property]) && !isset($element['#attributes'][$attribute])) {
            $element['#attributes'][$attribute] = $element[$property];
        }
    }
}

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