You are here

public function FlexiformElementField_entityreference::autocompleteValidate in Flexiform 7

Validate any autocomplete input.

See also

_entityreference_autocomplete_validate

File

includes/element/fields/entityreference.inc, line 75
Contains FlexiformElementField_entityreference class.

Class

FlexiformElementField_entityreference
Class for entityreference Field API elements.

Code

public function autocompleteValidate($element, &$form_state, $form) {
  $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 = $this
        ->getField();
      $instance = $this
        ->getInstance();
      $handler = entityreference_get_selection_handler($field, $instance, $element['#entity_type'], $element['#entity']);
      $value = $handler
        ->validateAutocompleteInput($element['#value'], $element, $form_state, $form);
    }
  }
  form_set_value($element, $value, $form_state);
}