function _form_set_attributes

Sets a form element's class attribute.

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

Parameters

$element: The form element.

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

Related topics

17 calls to _form_set_attributes()
form_pre_render_checkbox in drupal/core/includes/form.inc
Prepares a #type 'checkbox' render element for theme_input().
form_pre_render_color in drupal/core/includes/form.inc
Prepares a #type 'color' render element for theme_input().
form_pre_render_email in drupal/core/includes/form.inc
Prepares a #type 'email' render element for theme_input().
form_pre_render_file in drupal/core/includes/form.inc
Prepares a #type 'file' render element for theme_input().
form_pre_render_number in drupal/core/includes/form.inc
Prepares a #type 'number' render element for theme_input().

... See full list

File

drupal/core/includes/form.inc, line 4836
Functions for form and batch generation and processing.

Code

function _form_set_attributes(&$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
  // form_builder().
  if (!empty($element['#required'])) {
    $element['#attributes']['class'][] = 'required';
    $element['#attributes']['required'] = 'required';
    $element['#attributes']['aria-required'] = 'true';
  }
  if (isset($element['#parents']) && form_get_error($element) !== NULL && !empty($element['#validated'])) {
    $element['#attributes']['class'][] = 'error';
  }
}