function unique_field_taxonomy_term_form_validate in Unique field 8.2
Validate the taxonomy term for unique field conditions on submission.
1 string reference to 'unique_field_taxonomy_term_form_validate'
- unique_field_form_alter in ./
unique_field.module - Hook_form_BASE_FORM_ID_alter().
File
- ./
unique_field.module, line 364
Code
function unique_field_taxonomy_term_form_validate(array &$form, FormStateInterface $form_state) {
// If override permission set show warring message and skip validation.
if ($form_state
->getValues()['unique_field_override'] == 1) {
return;
}
$config = \Drupal::configFactory()
->get('unique_field.settings');
$fields = $config
->get('unique_field_taxonomy.' . $form['bundle'] . '.fields');
$scope = $config
->get('unique_field_taxonomy.' . $form['bundle'] . '.scope');
$comp_type = $config
->get('unique_field_taxonomy.' . $form['bundle'] . '.comp');
$name = $form_state
->getValues()['name'][0]['value'];
$langcode = $form_state
->getValues()['langcode'][0]['value'];
$description = $form_state
->getValues()['description'][0]['value'];
foreach ($fields as $field_name) {
if ($comp_type == UNIQUE_FIELD_COMP_EACH) {
$query = db_select('taxonomy_term_field_data', 't');
$query
->fields('t', array(
'tid',
));
}
$query = db_select('taxonomy_term_field_data', 't');
$query
->fields('t', array(
'tid',
));
if ($field_name == 'name') {
$query
->condition('t.name', $name);
}
elseif ($field_name == 'description') {
$query
->condition('t.description__value', $description);
}
else {
$field_type = \Drupal\field\Entity\FieldStorageConfig::loadByName('taxonomy_term', $field_name)
->get('type');
$values = $form_state
->getValues()[$field_name];
$field_values = array();
foreach ($values as $value) {
$field_values[] = $field_type == 'entity_reference' ? $value['target_id'] : $value['value'];
}
if ($scope == UNIQUE_FIELD_SCOPE_TERM) {
$unique_values = array_unique($field_values);
if (count($field_values) != count($unique_values)) {
$form_state
->setErrorByName($field_name, t('The field @field_name has to be unique.', array(
'@field_name' => $field_name,
)));
$form_state
->setRebuild();
continue;
}
}
$table_name = 'taxonomy_term__' . $field_name;
$col_name = $field_type == 'entity_reference' ? $field_name . '_target_id' : $field_name . '_value';
$query
->join($table_name, 'f', 't.tid = f.entity_id');
$query
->condition('f.' . $col_name, $field_values, 'IN');
if ($scope == UNIQUE_FIELD_SCOPE_LANGUAGE) {
$query
->condition('f.langcode', $langcode);
}
}
if ($scope == UNIQUE_FIELD_SCOPE_VOCABULARY) {
$query
->condition('t.vid', $form['bundle']);
}
if ($form_state
->getValues()['tid']) {
$query
->condition('t.tid', $form_state
->getValues()['tid'], '<>');
}
$res = $query
->execute()
->fetchField();
if ($res) {
$form_state
->setErrorByName($field_name, t('The field @field_name has to be unique.', array(
'@field_name' => $field_name,
)));
$user = \Drupal::currentUser();
if ($user
->hasPermission('unique_field_perm_bypass')) {
$form_id = str_replace('_', '-', $form['#id']);
$msg = t('Your form has unique field validation errors. Click !here to bypass this check and resubmit.', array(
'!here' => "<a href=\"#\" onclick=\"jQuery('form#" . $form_id . " input[name=\\'unique_field_override\\']').val(1);jQuery('form#" . $form_id . "').submit();return false;\">" . t('here') . '</a>',
));
drupal_set_message($msg, 'warning');
}
$form_state
->setRebuild();
}
}
}