You are here

public static function RenderElement::setAttributes in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Render/Element/RenderElement.php \Drupal\Core\Render\Element\RenderElement::setAttributes()

Sets a form element's class attribute.

Adds 'required' and 'error' classes as needed.

Parameters

array $element: The form element.

array $class: Array of new class names to be added.

Overrides ElementInterface::setAttributes

15 calls to RenderElement::setAttributes()
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.
Email::preRenderEmail in core/lib/Drupal/Core/Render/Element/Email.php
Prepares a #type 'email' render element for input.html.twig.

... See full list

File

core/lib/Drupal/Core/Render/Element/RenderElement.php, line 134
Contains \Drupal\Core\Render\Element\RenderElement.

Class

RenderElement
Provides a base class for render element plugins.

Namespace

Drupal\Core\Render\Element

Code

public static function setAttributes(&$element, $class = array()) {
  if (!empty($class)) {
    if (!isset($element['#attributes']['class'])) {
      $element['#attributes']['class'] = array();
    }
    $element['#attributes']['class'] = array_merge($element['#attributes']['class'], $class);
  }

  // This function is invoked from form element theme functions, but the
  // rendered form element may not necessarily have been processed by
  // \Drupal::formBuilder()->doBuildForm().
  if (!empty($element['#required'])) {
    $element['#attributes']['class'][] = 'required';
    $element['#attributes']['required'] = 'required';
    $element['#attributes']['aria-required'] = 'true';
  }
  if (isset($element['#parents']) && isset($element['#errors']) && !empty($element['#validated'])) {
    $element['#attributes']['class'][] = 'error';
    $element['#attributes']['aria-invalid'] = 'true';
  }
}