Skip to content
Snippets Groups Projects

Issue #3014694: Avoid duplicate entry for mass adding terms

2 files
+ 48
2
Compare changes
  • Side-by-side
  • Inline
Files
2
@@ -89,6 +89,12 @@ class AddTermsToVocabularyForm extends FormBase {
'#title' => $this->t('Create the term in the order provided in the list.'),
'#description' => $this->t('All terms will be added after the last existing term.'),
];
$form['ignore_duplicates'] = [
'#type' => 'checkbox',
'#title' => $this->t('Ignore duplicate terms.'),
'#description' => $this->t('Terms which already exist will not be recreated. New child terms of the duplicate will be placed under the existing Taxonomy term.'),
'#default_value' => TRUE,
];
$form['add'] = [
'#type' => 'submit',
'#value' => $this->t('Add'),
@@ -105,13 +111,15 @@ class AddTermsToVocabularyForm extends FormBase {
public function submitForm(array &$form, FormStateInterface $form_state) {
$term_names_too_long = [];
$term_names = [];
$duplicate_terms = [];
$taxonomy_vocabulary = $form_state->getValue('voc');
$parents = $form_state->getValue('parents');
$mass_terms = $form_state->getValue('mass_add');
$keep_order = $form_state->getValue('keep_order');
$ignore_duplicates = $form_state->getValue('ignore_duplicates');
$new_terms = $this->taxonomyManagerHelper->massAddTerms($mass_terms, $taxonomy_vocabulary->id(), $parents, $term_names_too_long, $keep_order);
$new_terms = $this->taxonomyManagerHelper->massAddTerms($mass_terms, $taxonomy_vocabulary->id(), $parents, $term_names_too_long, $keep_order, $ignore_duplicates, $duplicate_terms);
foreach ($new_terms as $term) {
$term_names[] = $term->label();
}
@@ -124,6 +132,9 @@ class AddTermsToVocabularyForm extends FormBase {
'%names' => implode(', ', $term_names_too_long),
]));
}
if (!empty($duplicate_terms)) {
$this->messenger()->addWarning($this->t('Skipped the creation of duplicate taxonomy term(s): %duplicate_terms', ['%duplicate_terms' => implode(', ', $duplicate_terms)]));
}
$this->messenger()->addMessage($this->t("Terms added: %terms", ['%terms' => implode(', ', $term_names)]));
$form_state->setRedirect('taxonomy_manager.admin_vocabulary', ['taxonomy_vocabulary' => $taxonomy_vocabulary->id()]);
}
Loading