You are here

public static function Select2::validateEntityAutocomplete in Select 2 8

Form element validation handler for entity_autocomplete elements.

Parameters

array $element: The render element.

\Drupal\Core\Form\FormStateInterface $form_state: The form state object.

array $complete_form: The form array.

File

src/Element/Select2.php, line 150

Class

Select2
Provides an select2 form element.

Namespace

Drupal\select2\Element

Code

public static function validateEntityAutocomplete(array &$element, FormStateInterface $form_state, array &$complete_form) {
  if ($element['#target_type'] && !$element['#autocreate']) {
    $value_callable = isset($element['#autocomplete_options_callback']) ? $element['#autocomplete_options_callback'] : NULL;
    if (!$value_callable || !is_callable($value_callable)) {
      $value_callable = '\\Drupal\\select2\\Element\\Select2::getValidSelectedOptions';
    }
    $value = [];
    $input_values = call_user_func_array($value_callable, [
      $element,
      $form_state,
    ]);
    foreach ($input_values as $id => $input) {
      $value[] = [
        'target_id' => $id,
      ];
    }
    $form_state
      ->setValueForElement($element, $value);
  }
}