You are here

function noderefcreate_autocomplete_validate in Node Reference Create 7

Same name and namespace in other branches
  1. 6 noderefcreate.module \noderefcreate_autocomplete_validate()

Validation callback for a noderefcreate autocomplete element.

This is mostly a copy of node_reference validate function, and we just create new node in case no node with provided title exists.

1 string reference to 'noderefcreate_autocomplete_validate'
noderefcreate_field_widget_form in ./noderefcreate.module
Implements hook_field_widget_form().

File

./noderefcreate.module, line 87
Defines a widget type for referencing one node from another and creating a new one if the provided doesn't exists.

Code

function noderefcreate_autocomplete_validate($element, &$form_state, $form) {
  $field = field_widget_field($element, $form_state);
  $instance = field_widget_instance($element, $form_state);
  $value = $element['#value'];
  $nid = NULL;
  if (strlen(trim($value)) > 0) {

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

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

      // No explicit nid (the submitted value was not populated by autocomplete
      // selection). Get the nid of a referencable node from the entered title.
      $options = array(
        'string' => $value,
        'match' => 'equals',
        'ids' => NULL,
        'limit' => 1,
      );
      $reference = node_reference_potential_references($field, $options);
      if ($reference) {

        // @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);
      }
      else {
        $newnode = NULL;
        if (is_array($field['settings']['referenceable_types'])) {
          foreach (array_filter($field['settings']['referenceable_types']) as $related_type) {
            $newnode->type = $related_type;
          }
        }
        global $user;
        $newnode->uid = $user->uid;
        $newnode->title = $value;
        $newnode->language = $element['#language'];
        if (module_exists('comment')) {
          $newnode->comment = variable_get("comment_{$newnode->type}", COMMENT_NODE_OPEN);
        }
        node_save($newnode);
        $nid = $newnode->nid;
      }
    }
  }
  else {
    if (strlen($value) > 0) {

      // if we got here then the user filled the name with spaces
      form_error($element, t('%name: title is empty. Please check your input.', array(
        '%name' => $instance['label'],
      )));
    }
  }

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