MoveTermsForm.php in Taxonomy Manager 2.0.x
File
src/Form/MoveTermsForm.php
View source
<?php
namespace Drupal\taxonomy_manager\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\taxonomy\TermStorage;
use Drupal\taxonomy\VocabularyInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class MoveTermsForm extends FormBase {
protected $termStorage;
public function __construct(TermStorage $termStorage) {
$this->termStorage = $termStorage;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager')
->getStorage('taxonomy_term'));
}
public function buildForm(array $form, FormStateInterface $form_state, VocabularyInterface $taxonomy_vocabulary = NULL, $selected_terms = []) {
if (empty($selected_terms)) {
$form['info'] = [
'#markup' => $this
->t('Please select the terms you want to move.'),
];
return $form;
}
$form_state
->setCached(TRUE);
$form['voc'] = [
'#type' => 'value',
'#value' => $taxonomy_vocabulary,
];
$form['selected_terms']['#tree'] = TRUE;
$items = [];
foreach ($this->termStorage
->loadMultiple($selected_terms) as $term) {
$items[] = $term
->label();
$form['selected_terms'][$term
->id()] = [
'#type' => 'value',
'#value' => $term
->id(),
];
}
$form['terms'] = [
'#theme' => 'item_list',
'#items' => $items,
'#title' => $this
->t('Selected terms to move:'),
];
$form['keep_old_parents'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Keep old parents and add new ones (multi-parent). Otherwise old parents get replaced.'),
];
$form['delete'] = [
'#type' => 'submit',
'#value' => $this
->t('Move'),
];
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$taxonomy_vocabulary = $form_state
->getValue('voc');
$this
->messenger()
->addError($this
->t('Move operation not yet implemented.'));
$form_state
->setRedirect('taxonomy_manager.admin_vocabulary', [
'taxonomy_vocabulary' => $taxonomy_vocabulary
->id(),
]);
}
public function getFormId() {
return 'taxonomy_manager_move_form';
}
}