You are here

function i18n_taxonomy_autocomplete_validate in Internationalization 7

Form element validate handler for taxonomy term autocomplete element.

1 string reference to 'i18n_taxonomy_autocomplete_validate'
i18n_taxonomy_translation_term_form in i18n_taxonomy/i18n_taxonomy.admin.inc
Produces a vocabulary translation form.

File

i18n_taxonomy/i18n_taxonomy.admin.inc, line 84
Helper functions for taxonomy administration.

Code

function i18n_taxonomy_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']) {

    // Collect candidate vocabularies.
    $vocabulary = $form_state['values']['vocabulary'];
    $vocabularies[$vocabulary->vid] = $vocabulary;

    // Translate term names into actual terms.
    $typed_terms = drupal_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),
        'vid' => $vocabulary->vid,
        'language' => $element['#langcode'],
      ))) {
        $term = array_pop($possibilities);
      }
      else {
        $vocabulary = reset($vocabularies);
        $term = array(
          'tid' => 'autocreate',
          'vid' => $vocabulary->vid,
          'name' => $typed_term,
          'vocabulary_machine_name' => $vocabulary->machine_name,
          'language' => $element['#langcode'],
        );
      }
      $value[] = (array) $term;
    }
  }
  form_set_value($element, $value, $form_state);
}