You are here

public static function EntitySelect::validateEntitySelect in Commerce Core 8.2

Validation callback.

Transforms the subelement value into a consistent format and set it on the main element.

File

src/Element/EntitySelect.php, line 150

Class

EntitySelect
Provides a form input element for selecting one or multiple entities.

Namespace

Drupal\commerce\Element

Code

public static function validateEntitySelect(&$element, FormStateInterface $form_state, &$complete_form) {
  $value_element = $element['value'];
  $value = $form_state
    ->getValue($value_element['#parents']);
  if (is_array($value)) {
    if ($value_element['#type'] == 'checkboxes') {

      // Remove unselected checkboxes.
      $value = array_filter($value);

      // Non-numeric keys can cause issues when passing values to TypedData.
      $value = array_values($value);
    }
    elseif (!empty($value_element['#tags'])) {

      // Extract the entity ids from a multivalue autocomplete.
      $value = array_column($value, 'target_id');
    }
  }
  $form_state
    ->setValueForElement($element, $value);
}