You are here

public static function Element::setAttributes in Render cache 7.2

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('#propertyname' => 'attributename'). If both names are identical except for the leading '#', then an attribute name value is sufficient and no property name needs to be specified.

File

lib/Drupal/Core/Render/Element.php, line 156
Contains \Drupal\Core\Render\Element.

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