You are here

function views_ui_taxonomy_autocomplete_validate in Views (for Drupal 7) 7.3

Same name and namespace in other branches
  1. 8.3 views_ui/admin.inc \views_ui_taxonomy_autocomplete_validate()

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()

1 string reference to 'views_ui_taxonomy_autocomplete_validate'
ViewsUiBaseViewsWizard::build_filters in plugins/views_wizard/views_ui_base_views_wizard.class.php
Build the part of the form that allows the user to select the filters.

File

includes/admin.inc, line 788
Provides the Views' administrative interface.

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'])) {
      foreach ($field['settings']['allowed_values'] as $tree) {
        if ($vocabulary = taxonomy_vocabulary_machine_name_load($tree['vocabulary'])) {
          $vocabularies[$vocabulary->vid] = $tree['vocabulary'];
        }
      }
    }

    // Store the term ID of each (valid) tag that the user typed.
    $typed_terms = drupal_explode_tags($tags);
    foreach ($typed_terms as $typed_term) {
      if ($terms = taxonomy_term_load_multiple(array(), array(
        'name' => trim($typed_term),
        'vid' => array_keys($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);
}