You are here

public static function SynonymsEntitySelect::validateEntitySelect in Synonyms 8

Form element validation handler for synonyms_entity_select elements.

File

src/Element/SynonymsEntitySelect.php, line 111

Class

SynonymsEntitySelect
Form element for synonyms-friendly entity select.

Namespace

Drupal\synonyms\Element

Code

public static function validateEntitySelect(array &$element, FormStateInterface $form_state, array &$complete_form) {
  $value = $form_state
    ->getValue($element['#parents']);
  if (!isset($element['#multiple']) || !$element['#multiple']) {
    $value = [
      $value,
    ];
  }
  $unique = [];
  foreach ($value as $v) {
    if (!empty($v)) {
      if (!is_numeric($v)) {
        $v = explode(self::DELIMITER, $v, 2)[0];
      }
      $unique[$v] = $v;
    }
  }
  $items = [];
  foreach ($unique as $v) {
    $items[] = [
      $element['#key_column'] => $v,
    ];
  }
  if (!isset($element['#multiple']) || !$element['#multiple']) {
    $items = reset($items);
  }
  $form_state
    ->setValueForElement($element, $items);
}