You are here

function content_taxonomy_et_autocomplete_validate in Content Taxonomy 7

Replacement for form validate handler taxonomy_autocomplete_validate().

Uses content_taxonomy_et_get_first_possible_term() to retrieve the right term in the right language, instead of term_load_multiple().

1 string reference to 'content_taxonomy_et_autocomplete_validate'
content_taxonomy_et_field_widget_form_alter in ./content_taxonomy_et.module
Implements hook_field_widget_form_alter().

File

./content_taxonomy_et.module, line 166
Entity Translation integration for taxonomy terms.

Code

function content_taxonomy_et_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.
    $field = field_widget_field($element, $form_state);
    $vocabularies = array();
    foreach ($field['settings']['allowed_values'] as $tree) {
      if ($vocabulary = taxonomy_vocabulary_machine_name_load($tree['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.
      $term = content_taxonomy_et_get_first_possible_term(trim($typed_term), array_keys($vocabularies));
      if (!$term) {
        $vocabulary = reset($vocabularies);
        $term = array(
          'tid' => 'autocreate',
          'vid' => $vocabulary->vid,
          'name' => $typed_term,
          'vocabulary_machine_name' => $vocabulary->machine_name,
        );
      }
      $value[] = (array) $term;
    }
  }
  form_set_value($element, $value, $form_state);
}