1 taxonomy.module taxonomy_autocomplete_validate($element, &$form_state)

Form element validate handler for taxonomy term autocomplete element.

File

core/modules/taxonomy/taxonomy.module, line 1880
Enables the organization of content into categories.

Code

function taxonomy_autocomplete_validate($element, &$form_state) {
  $langcode = LANGUAGE_NONE;
  if (isset($form_state['values']['langcode'])) {
    $langcode = $form_state['values']['langcode'];
  }
  // 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']) {
    // Collect candidate vocabularies.
    $field = field_widget_field($element, $form_state);
    $instance = field_widget_instance($element, $form_state);
    $vocabularies = array();
    foreach ($field['settings']['allowed_values'] as $tree) {
      if ($vocabulary = taxonomy_vocabulary_load($tree['vocabulary'])) {
        $vocabularies[$vocabulary->machine_name] = $vocabulary;
      }
    }

    // Translate term names into actual terms.
    $typed_terms = backdrop_explode_tags($tags);
    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), 'vocabulary' => array_keys($vocabularies)))) {
        $term = array_pop($possibilities);
      }
      else {
        $vocabulary = reset($vocabularies);
        $term = array(
          'tid' => 'autocreate',
          'name' => $typed_term,
          'vocabulary' => $vocabulary->machine_name,
          'langcode' => LANGUAGE_NONE,
        );
        if (module_exists('language') && $vocabulary->language == TAXONOMY_LANGUAGE_ENABLED && $instance['widget']['settings']['follow_content_language']) {
          $term['langcode'] = $langcode;
        }
      }
      $value[] = (array) $term;
    }
  }

  form_set_value($element, $value, $form_state);
}