You are here

function _entityreference_autocomplete_tags_validate in Entity reference 7

Implements hook_validate().

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

File

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

Code

function _entityreference_autocomplete_tags_validate($element, &$form_state, $form) {
  $value = array();

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

      // Take "label (entity id)', match the id from parenthesis.
      if (preg_match("/.+\\((\\d+)\\)/", $entity, $matches)) {
        $value[] = array(
          'target_id' => $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);
        $value[] = array(
          'target_id' => $handler
            ->validateAutocompleteInput($entity, $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);
}