You are here

public static function EntityAutocomplete::validateEntityAutocomplete in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/Plugin/WebformElement/EntityAutocomplete.php \Drupal\webform\Plugin\WebformElement\EntityAutocomplete::validateEntityAutocomplete()

Form API callback. Remove target id property and create an array of entity ids.

File

src/Plugin/WebformElement/EntityAutocomplete.php, line 125

Class

EntityAutocomplete
Provides a 'entity_autocomplete' element.

Namespace

Drupal\webform\Plugin\WebformElement

Code

public static function validateEntityAutocomplete(array &$element, FormStateInterface $form_state) {

  // Must use ::getValue($element['#parents']) because $element['#value'] is
  // not being updated.
  // @see \Drupal\Core\Entity\Element\EntityAutocomplete::validateEntityAutocomplete
  $value = $form_state
    ->getValue($element['#parents']);
  if (empty($value) || !is_array($value)) {
    return;
  }

  /** @var \Drupal\webform\Plugin\WebformElementManagerInterface $element_manager */
  $element_manager = \Drupal::service('plugin.manager.webform.element');
  $element_plugin = $element_manager
    ->getElementInstance($element);
  if ($element_plugin
    ->hasMultipleValues($element)) {
    $entity_ids = [];
    foreach ($value as $item) {
      $entity_ids[] = (string) static::getEntityIdFromItem($item);
    }
    $form_state
      ->setValueForElement($element, $entity_ids);
  }
  else {
    $form_state
      ->setValueForElement($element, (string) static::getEntityIdFromItem($value));
  }
}