You are here

public static function Select2::valueCallback in Select 2 8

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 Select::valueCallback

File

src/Element/Select2.php, line 127

Class

Select2
Provides an select2 form element.

Namespace

Drupal\select2\Element

Code

public static function valueCallback(&$element, $input, FormStateInterface $form_state) {

  // Potentially the #value is set directly, so it contains the 'target_id'
  // array structure instead of a string.
  if ($input !== FALSE && is_array($input)) {
    $input = array_map(function ($item) {
      return isset($item['target_id']) ? $item['target_id'] : $item;
    }, $input);
    return array_combine($input, $input);
  }
  return parent::valueCallback($element, $input, $form_state);
}