View source
<?php
define('TAXONOMY_UNIQUE_DEFAULT_MESSAGE', 'Term @term already exists in vocabulary @vocabulary.');
function taxonomy_unique_form_taxonomy_vocabulary_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
$form['unique_container'] = array(
'#type' => 'fieldset',
'#title' => t('Taxonomy unique'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$unique_default_value = \Drupal::config('taxonomy_unique.settings')
->get($form_state
->getFormObject()
->getEntity()
->id());
if (is_array($unique_default_value) || empty($unique_default_value)) {
$unique_default_value = FALSE;
}
$form['unique_container']['unique'] = array(
'#type' => 'checkbox',
'#title' => t('Terms should be unique.'),
'#default_value' => $unique_default_value,
);
$unique_message_default_value = \Drupal::config('taxonomy_unique.settings')
->get($form_state
->getFormObject()
->getEntity()
->id() . '_message');
if (empty($unique_message_default_value)) {
$unique_message_default_value = TAXONOMY_UNIQUE_DEFAULT_MESSAGE;
}
$form['unique_container']['unique_message'] = array(
'#type' => 'textfield',
'#title' => t('Message to show if term already exists'),
'#description' => t('Placeholders: %term and %vocabulary'),
'#default_value' => $unique_message_default_value,
);
$form['actions']['submit']['#submit'][] = 'taxonomy_unique_taxonomy_form_vocabulary_submit';
}
function taxonomy_unique_taxonomy_form_vocabulary_submit($form, \Drupal\Core\Form\FormStateInterface $form_state) {
\Drupal::configFactory()
->getEditable('taxonomy_unique.settings')
->set($form_state
->getFormObject()
->getEntity()
->id(), $form_state
->getValue('unique'))
->set($form_state
->getFormObject()
->getEntity()
->id() . '_message', $form_state
->getValue('unique_message'))
->save();
}
function taxonomy_unique_form_taxonomy_term_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
if (\Drupal::config('taxonomy_unique.settings')
->get($form_state
->getFormObject()
->getEntity()
->getVocabularyId())) {
$form['#validate'][] = 'taxonomy_unique_term_name_validate';
}
}
function taxonomy_unique_term_name_validate($form, \Drupal\Core\Form\FormStateInterface $form_state) {
$triggering_element = $form_state
->getTriggeringElement();
if ($triggering_element['#button_type'] != 'primary') {
return;
}
$name = $form_state
->getValue('name');
$name = $name[0]['value'];
$vocabulary_machine_name = $form_state
->getFormObject()
->getEntity()
->getVocabularyId();
$tid = $form_state
->getFormObject()
->getEntity()
->id();
if ($name != '' && !taxonomy_unique_is_term_unique($name, $vocabulary_machine_name, $tid)) {
$error_message = \Drupal::config('taxonomy_unique.settings')
->get($form_state
->getFormObject()
->getEntity()
->getVocabularyId() . '_message');
if (empty($error_message)) {
$error_message = TAXONOMY_UNIQUE_DEFAULT_MESSAGE;
}
$form_state
->setErrorByName('name', \Drupal\Component\Utility\Xss::filter(\Drupal\Component\Utility\SafeMarkup::format($error_message, array(
'@term' => $name,
'@vocabulary' => $vocabulary_machine_name,
))));
}
}
function taxonomy_unique_is_term_unique($term_name, $vocabulary_machine_name, $tid = NULL) {
$terms = \Drupal::entityTypeManager()
->getStorage('taxonomy_term')
->loadByProperties([
'name' => $term_name,
'vid' => $vocabulary_machine_name,
]);
$term = current($terms);
if (empty($terms) || count($terms) == 1 && $term
->id() == $tid) {
return TRUE;
}
return FALSE;
}