function taxonomy_autocomplete_validate

Form element validate handler for taxonomy term autocomplete element.

1 string reference to 'taxonomy_autocomplete_validate'
taxonomy_field_widget_form in modules/taxonomy/taxonomy.module
Implements hook_field_widget_form().

File

modules/taxonomy/taxonomy.module, line 1757

Code

function 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.
        $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.
            if ($possibilities = taxonomy_term_load_multiple(array(), array(
                'name' => trim($typed_term),
                'vid' => array_keys($vocabularies),
            ))) {
                $term = array_pop($possibilities);
            }
            else {
                $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);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.