You are here

function _entityreference_autocomplete_validate in Entity reference 7

Implements hook_validate().

1 string reference to '_entityreference_autocomplete_validate'
entityreference_field_widget_form in ./entityreference.module
Implements hook_field_widget_form().

File

./entityreference.module, line 934
Entityreference primary module file.

Code

function _entityreference_autocomplete_validate($element, &$form_state, $form) {

  // 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']);
      $handler = entityreference_get_selection_handler($field);
      $field_name = $element['#field_name'];
      $field = field_info_field($field_name);
      $instance = field_info_instance($element['#entity_type'], $field_name, $element['#bundle']);
      $handler = entityreference_get_selection_handler($field, $instance);
      $value = $handler
        ->validateAutocompleteInput($element['#value'], $element, $form_state, $form);
    }
  }

  // Update the value of this element so the field can validate the product IDs.
  form_set_value($element, $value, $form_state);
}