1.20.x views_ui.admin.inc views_ui_taxonomy_autocomplete_validate($element, &$form_state)

Form element validation handler for a taxonomy autocomplete field.

This allows a taxonomy autocomplete field to be validated outside the standard Field API workflow, without passing in a complete field widget. Instead, all that is required is that $element['#field_name'] contain the name of the taxonomy autocomplete field that is being validated.

This function is currently not used for validation directly, although it could be. Instead, it is only used to store the term IDs and vocabulary name in the element value, based on the tags that the user typed in.

See also

taxonomy_autocomplete_validate()

File

modules/views_ui/views_ui.admin.inc, line 863
Admin page callbacks for the Views UI module.

Code

function views_ui_taxonomy_autocomplete_validate($element, &$form_state) {
  $value = array();
  if ($tags = $element['#value']) {
    // Get the machine names of the vocabularies we will search, keyed by the
    // vocabulary IDs.
    $field = field_info_field($element['#field_name']);
    $vocabularies = array();
    if (!empty($field['settings']['allowed_values'])) {
      $vocabularies = $field['settings']['allowed_values'];
      foreach ($vocabularies as $index => $settings) {
        $vocabularies[$index] = $settings['vocabulary'];
      }
    }
    // Store the term ID of each (valid) tag that the user typed.
    $typed_terms = backdrop_explode_tags($tags);
    foreach ($typed_terms as $typed_term) {
      if ($terms = taxonomy_term_load_multiple(array(), array('name' => trim($typed_term), 'vocabulary' => $vocabularies))) {
        $term = array_pop($terms);
        $value['tids'][] = $term->tid;
      }
    }
    // Store the term IDs along with the name of the vocabulary. Currently
    // Views (as well as the Field UI) assumes that there will only be one
    // vocabulary, although technically the API allows there to be more than
    // one.
    if (!empty($value['tids'])) {
      $value['tids'] = array_unique($value['tids']);
      $value['vocabulary'] = array_pop($vocabularies);
    }
  }
  form_set_value($element, $value, $form_state);
}