You are here

public static function Select::valueCallback in Drupal 8

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Render/Element/Select.php \Drupal\Core\Render\Element\Select::valueCallback()

Determines how user input is mapped to an element's #value property.

Parameters

array $element: An associative array containing the properties of the element.

mixed $input: The incoming input to populate the form element. If this is FALSE, the element's default value should be returned.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

mixed The value to assign to the element.

Overrides FormElement::valueCallback

File

core/lib/Drupal/Core/Render/Element/Select.php, line 164

Class

Select
Provides a form element for a drop-down menu or scrolling selection box.

Namespace

Drupal\Core\Render\Element

Code

public static function valueCallback(&$element, $input, FormStateInterface $form_state) {
  if ($input !== FALSE) {
    if (isset($element['#multiple']) && $element['#multiple']) {

      // If an enabled multi-select submits NULL, it means all items are
      // unselected. A disabled multi-select always submits NULL, and the
      // default value should be used.
      if (empty($element['#disabled'])) {
        return is_array($input) ? array_combine($input, $input) : [];
      }
      else {
        return isset($element['#default_value']) && is_array($element['#default_value']) ? $element['#default_value'] : [];
      }
    }
    elseif (isset($element['#empty_value']) && $input === (string) $element['#empty_value']) {
      return $element['#empty_value'];
    }
    else {
      return $input;
    }
  }
}