1

I've got a form for non logged in users to submit an event, which then goes in to a moderation queue via rules. At the moment, when an authenticated user adds an event, they've got the option to either pick a venue from a list of already populated nodes (field_venue), or specify a venue in plain text if the venue isn't a node (field_venuetext).

I'd like to make this a bit easier for non-authenticated users and just provide one field which is a node reference autocomplete field. If the user just types a venue and doesn't select from the autocomplete, it will populate the plain text field (field_venuetext). Looking at the nodereference.module source, I can see the function I'd like to change (nodereference_autocomplete_validate), and I guess, I'd do something like this:

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)) {
        // *** CHANGE *** At the moment, this generates an error, but I would like to populate the field 'field_venuetext'
        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);
}

However I'm not sure how I'd override it, or indeed how I'd populate the field field_venuetext. I'm new to Drupal, so not 100% sure where to go next. Any ideas?

0

After some poking around and asking on the Drupal forum, I've found that there's a module that does pretty much what I want

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.