function references_dialog_term_reference_autocomplete_validate in References dialog 7
Form element validate handler for taxonomy term autocomplete element.
1 string reference to 'references_dialog_term_reference_autocomplete_validate'
- references_dialog_field_widget_form in ./
references_dialog.module - Implements hook_field_widget_form().
File
- ./
references_dialog.module, line 913 - This the main module file.
Code
function references_dialog_term_reference_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 = NULL;
if ($element['#value']) {
$term = FALSE;
$field = field_widget_field($element, $form_state);
$instance = field_widget_instance($element, $form_state);
// Check whether we have an explicit "[tid:n]" input.
preg_match('/^(?:\\s*|(.*) )?\\[\\s*tid\\s*:\\s*(\\d+)\\s*\\]$/', $element['#value'], $matches);
if (!empty($matches)) {
// Explicit tid.
list(, $term_name, $tid) = $matches;
if (!empty($term_name)) {
$term = taxonomy_term_load($tid);
}
}
else {
// Collect candidate vocabularies.
$vocabularies = array();
foreach ($field['settings']['allowed_values'] as $tree) {
if ($vocabulary = taxonomy_vocabulary_machine_name_load($tree['vocabulary'])) {
$vocabularies[$vocabulary->vid] = $vocabulary;
}
}
if ($possibilities = taxonomy_term_load_multiple(array(), array(
'name' => trim($element['#value']),
'vid' => array_keys($vocabularies),
))) {
$term = array_pop($possibilities);
}
}
if ($term) {
$value = $term->tid;
}
else {
form_error($element, t('%name: found no valid post for this value.', array(
'%name' => $instance['label'],
)));
}
}
form_set_value($element, $value, $form_state);
}