You are here

function nodereference_autocomplete_validate in Content Construction Kit (CCK) 6

Same name and namespace in other branches
  1. 6.3 modules/nodereference/nodereference.module \nodereference_autocomplete_validate()
  2. 6.2 modules/nodereference/nodereference.module \nodereference_autocomplete_validate()

Validate an autocomplete element.

Remove the wrapper layer and set the right element's value.

1 string reference to 'nodereference_autocomplete_validate'
nodereference_autocomplete_process in modules/nodereference/nodereference.module
Process an individual element.

File

modules/nodereference/nodereference.module, line 487
Defines a field type for referencing one node from another.

Code

function nodereference_autocomplete_validate($element, &$form_state) {
  $field_name = $element['#field_name'];
  $field = content_fields($field_name);
  $field_key = $element['#columns'][0];
  $delta = $element['#delta'];
  $value = $element['#value'][$field_key];
  $nid = NULL;
  if (!empty($value)) {
    preg_match('/^(?:\\s*|(.*) )?\\[\\s*nid\\s*:\\s*(\\d+)\\s*\\]$/', $value, $matches);
    if (!empty($matches)) {

      // explicit nid
      list(, $title, $nid) = $matches;
      if (!empty($title) && ($n = node_load($nid)) && $title != $n->title) {
        form_set_error($element[$field_key], t('%name: Title mismatch. Please check your selection.'), array(
          '%name' => t($element[$field_key]['#title']),
        ));
      }
    }
    else {

      // no explicit nid
      // TODO :
      // the best thing would be to present the user with an additional form,
      // allowing the user to choose between valid candidates with the same title
      // ATM, we pick the first matching candidate...
      $nids = _nodereference_potential_references($field, FALSE, $value, TRUE);
      $nid = !empty($nids) ? array_shift(array_keys($nids)) : 0;
    }
  }
  form_set_value($element, $nid, $form_state);
  return $element;
}