public static function Select::valueCallback

public static Select::valueCallback(&$element, $input, FormStateInterface $form_state)

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 139

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) : array();
      }
      else {
        return (isset($element['#default_value']) && is_array($element['#default_value'])) ? $element['#default_value'] : array();
      }
    }
    // Non-multiple select elements may have an empty option prepended to them
    // (see \Drupal\Core\Render\Element\Select::processSelect()). When this
    // occurs, usually #empty_value is an empty string, but some forms set
    // #empty_value to integer 0 or some other non-string constant. PHP
    // receives all submitted form input as strings, but if the empty option
    // is selected, set the value to match the empty value exactly.
    elseif (isset($element['#empty_value']) && $input === (string) $element['#empty_value']) {
      return $element['#empty_value'];
    }
    else {
      return $input;
    }
  }
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Render!Element!Select.php/function/Select::valueCallback/8.1.x