You are here

function nodereference_autocomplete_validate in Content Construction Kit (CCK) 6.2

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

Validate an autocomplete element.

Remove the wrapper layer and set the right element's value. This will move the nested value at 'field-name-0-nid-nid' back to its original location, 'field-name-0-nid'.

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 727
Defines a field type for referencing one node from another.

Code

function nodereference_autocomplete_validate($element, &$form_state) {
  $field_name = $element['#field_name'];
  $type_name = $element['#type_name'];
  $field = content_fields($field_name, $type_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:n].
      list(, $title, $nid) = $matches;
      if (!empty($title) && ($n = node_load($nid)) && trim($title) != trim($n->title)) {
        form_error($element[$field_key], t('%name: title mismatch. Please check your selection.', array(
          '%name' => t($field['widget']['label']),
        )));
      }
    }
    else {

      // No explicit nid.
      $reference = _nodereference_potential_references($field, $value, 'equals', NULL, 1);
      if (empty($reference)) {
        form_error($element[$field_key], t('%name: found no valid post with that title.', array(
          '%name' => t($field['widget']['label']),
        )));
      }
      else {

        // 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...
        $nid = key($reference);
      }
    }
  }
  form_set_value($element, $nid, $form_state);
}