You are here

function notifications_tags_autocomplete_validate in Notifications 7

Form element validate handler for taxonomy term autocomplete element.

1 string reference to 'notifications_tags_autocomplete_validate'
notifications_scheduler_new_posts_action_form in notifications_scheduler/notifications_scheduler.module
Action: Configuration form

File

notifications_tags/notifications_tags.module, line 416
Subscriptions to taxonomy terms

Code

function notifications_tags_autocomplete_validate($element, &$form_state) {

  // Autocomplete widgets do not send their tids in the form, so we must detect
  // them here and process them independently.
  $value = array();
  if ($tags = $element['#value']) {
    $simple = $element['#autocomplete_path'] == 'notifications_tags/autocomplete/simple';

    // Translate term names into actual terms.
    $typed_terms = drupal_explode_tags($tags);

    // If a simple autocomplete, check we have just one tag
    if ($simple && count($typed_terms) > 1) {
      form_set_error(implode('][', $element['#parents']), t('This field admits only a single term'));
      return;
    }

    // Collect candidate vocabularies.
    $vids = notifications_tags_vocabulary_enabled();
    if (!$vids) {
      form_set_error(implode('][', $element['#parents']), t('You must have at least one vocabulary enabled for tag subscriptions.'));
      return;
    }
    foreach ($typed_terms as $typed_term) {

      // See if the term exists in the chosen vocabulary and return the tid;
      // otherwise, create a new 'autocreate' term for insert/update.
      if ($possibilities = taxonomy_term_load_multiple(array(), array(
        'name' => trim($typed_term),
        'vid' => $vids,
      ))) {
        $term = array_pop($possibilities);
        $value[] = $term->tid;
      }
    }
    if ($simple) {
      $value = reset($value);
    }
  }
  form_set_value($element, $value, $form_state);
}