View source
<?php
namespace Drupal\taxonomy_manager\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\taxonomy\VocabularyInterface;
use Drupal\taxonomy_manager\TaxonomyManagerHelper;
class AddTermsToVocabularyForm extends FormBase {
public function buildForm(array $form, FormStateInterface $form_state, VocabularyInterface $taxonomy_vocabulary = NULL, $parents = []) {
if ($this
->getRequest()
->getMethod() == 'POST') {
$form_state
->setCached(TRUE);
}
$form['voc'] = [
'#type' => 'value',
'#value' => $taxonomy_vocabulary,
];
$form['parents']['#tree'] = TRUE;
foreach ($parents as $p) {
$form['parents'][$p] = [
'#type' => 'value',
'#value' => $p,
];
}
$description = $this
->t("If you have selected one or more terms in the tree view, the new terms are automatically children of those.");
$form['help'] = [
'#markup' => $description,
];
$form['mass_add'] = [
'#type' => 'textarea',
'#title' => $this
->t('Terms'),
'#description' => $this
->t("One term per line. Child terms can be prefixed with a\n dash '-' (one dash per hierarchy level). Terms that should not become\n child terms and start with a dash need to be wrapped in double quotes.\n <br />Example:<br />\n animals<br />\n -canine<br />\n --dog<br />\n --wolf<br />\n -feline<br />\n --cat"),
'#rows' => 10,
'#required' => TRUE,
];
$form['add'] = [
'#type' => 'submit',
'#value' => $this
->t('Add'),
];
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$term_names_too_long = [];
$term_names = [];
$taxonomy_vocabulary = $form_state
->getValue('voc');
$parents = $form_state
->getValue('parents');
$mass_terms = $form_state
->getValue('mass_add');
$new_terms = TaxonomyManagerHelper::massAddTerms($mass_terms, $taxonomy_vocabulary
->id(), $parents, $term_names_too_long);
foreach ($new_terms as $term) {
$term_names[] = $term
->label();
}
if (count($term_names_too_long)) {
$this
->messenger()
->addWarning($this
->t("Following term names were too long and truncated to 255 characters: %names.", [
'%names' => implode(', ', $term_names_too_long),
]));
}
$this
->messenger()
->addMessage($this
->t("Terms added: %terms", [
'%terms' => implode(', ', $term_names),
]));
$form_state
->setRedirect('taxonomy_manager.admin_vocabulary', [
'taxonomy_vocabulary' => $taxonomy_vocabulary
->id(),
]);
}
public function getFormId() {
return 'taxonomy_manager.add_form';
}
}