function views_ui_taxonomy_autocomplete_validate in Views (for Drupal 7) 8.3
Same name and namespace in other branches
- 7.3 includes/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'
- Node::build_filters in lib/
Views/ node/ Plugin/ views/ wizard/ Node.php - Overrides Drupal\views\Plugin\views\wizard\WizardPluginBase::build_filters().
File
- views_ui/
admin.inc, line 396 - 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 = entity_load_multiple_by_properties('taxonomy_term', 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);
}