You are here

public function AutocompleteWidget::elementValidate in Entity reference 8

Implements Drupal\entityreference\Plugin\field\widget\DefaultAutocompleteWidget::elementValidate()

Overrides AutocompleteWidgetBase::elementValidate

File

lib/Drupal/entityreference/Plugin/field/widget/AutocompleteWidget.php, line 61
Definition of Drupal\entityreference\Plugin\field\widget\AutocompleteWidget.

Class

AutocompleteWidget
Plugin implementation of the 'entityreference autocomplete' widget.

Namespace

Drupal\entityreference\Plugin\field\widget

Code

public function elementValidate($element, &$form_state) {

  // If a value was entered into the autocomplete.
  $value = '';
  if (!empty($element['#value'])) {

    // Take "label (entity id)', match the id from parenthesis.
    if (preg_match("/.+\\((\\d+)\\)/", $element['#value'], $matches)) {
      $value = $matches[1];
    }
    else {

      // Try to get a match from the input string when the user didn't use the
      // autocomplete but filled in a value manually.
      $field = field_info_field($element['#field_name']);
      $instance = field_info_instance($element['#entity_type'], $element['#field_name'], $element['#bundle']);
      $handler = entityreference_get_selection_handler($field, $instance);
      $value = $handler
        ->validateAutocompleteInput($element['#value'], $element, $form_state, $form);
    }
  }
  form_set_value($element, $value, $form_state);
}