You are here

function nodequeue_reference_autocomplete_validate in Nodequeue reference 7

Validation callback for a nodequeue_reference autocomplete element.

1 string reference to 'nodequeue_reference_autocomplete_validate'
nodequeue_reference_field_widget_form in ./nodequeue_reference.module
Implements hook_field_widget_form().

File

./nodequeue_reference.module, line 285
Form field type for node queues.

Code

function nodequeue_reference_autocomplete_validate($element, &$form_state, $form) {
  $field = field_widget_field($element, $form_state);
  $instance = field_widget_instance($element, $form_state);
  $value = $element['#value'];
  $qid = NULL;
  if (!empty($value)) {

    // Check whether we have an explicit "[qid:n]" input.
    preg_match('/^(?:\\s*|(.*) )?\\[\\s*qid*:\\s*(\\d+)\\s*\\]$/', $value, $matches);
    if (!empty($matches)) {

      // Explicit qid. Check that the 'title' part matches the actual title for
      // the qid.
      list(, $title, $qid) = $matches;
      if (!empty($title)) {
        $real_title = db_select('nodequeue_queue', 'q')
          ->fields('q', array(
          'title',
        ))
          ->condition('q.qid', $qid)
          ->execute()
          ->fetchField();
        if (trim($title) != trim($real_title)) {
          form_error($element, t('%name: title mismatch. Please check your selection.', array(
            '%name' => $instance['label'],
          )));
        }
      }
    }
    else {
      form_error($element, t('%name: found no valid nodequeue with that title.', array(
        '%name' => $instance['label'],
      )));
    }
  }

  // Set the element's value as the nodequeue id that was extracted from the entered
  // input.
  form_set_value($element, $qid, $form_state);
}