function Select::valueCallback

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

Overrides FormElementBase::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;
        }
    }
}

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