You are here

function og_field_audience_autocomplete_validate in Organic groups 7

Validation callback for a group audience autocomplete element.

1 string reference to 'og_field_audience_autocomplete_validate'
og_field_widget_form in ./og.field.inc
Implements hook_field_widget_form().

File

./og.field.inc, line 690
Field module functionality for the Organic groups module.

Code

function og_field_audience_autocomplete_validate($element, &$form_state, $form) {
  $field = $form_state['field'][$element['#field_name']][$element['#language']]['field'];
  $instance = $form_state['field'][$element['#field_name']][$element['#language']]['instance'];
  $value = $element['#value'];
  $gid = NULL;
  if (!empty($value)) {

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

      // Explicit gid. Check that the 'title' part matches the actual title for
      // the nid.
      list(, $label, $gid) = $matches;
      if (!empty($label)) {
        if ($label != og_label($gid)) {
          form_error($element, t('%name: label mismatch. Please check your selection.', array(
            '%name' => $instance['label'],
          )));
        }
      }
    }
    else {

      // No explicit gid (the submitted value was not populated by autocomplete
      // selection). Get the gid of a referencable node from the entered title.
      if ($reference = og_field_audience_potential_groups($value, 'equals', NULL, 1)) {
        $gid = key($reference);
      }
      else {
        form_error($element, t('%name: found no valid group with that label.', array(
          '%name' => $instance['label'],
        )));
      }
    }
  }

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