MergeTermsTarget.php in Term Merge 8
File
src/Form/MergeTermsTarget.php
View source
<?php
namespace Drupal\term_merge\Form;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\TempStore\PrivateTempStoreFactory;
use Drupal\taxonomy\VocabularyInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class MergeTermsTarget extends FormBase {
protected $entityTypeManager;
protected $termStorage;
private $tempStoreFactory;
private $vocabulary;
public function __construct(EntityTypeManagerInterface $entityTypeManager, PrivateTempStoreFactory $tempStoreFactory) {
$this->entityTypeManager = $entityTypeManager;
$this->termStorage = $entityTypeManager
->getStorage('taxonomy_term');
$this->tempStoreFactory = $tempStoreFactory;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'), $container
->get('tempstore.private'));
}
public function getFormId() {
return 'taxonomy_merge_terms_target';
}
public function titleCallback() {
return $this
->t('Please select a target term');
}
public function buildForm(array $form, FormStateInterface $form_state, VocabularyInterface $taxonomy_vocabulary = NULL) {
$this->vocabulary = $taxonomy_vocabulary;
$form['description']['#markup'] = $this
->t('Please enter a new term or select an existing term to merge into.');
$form['new'] = [
'#type' => 'textfield',
'#title' => $this
->t('New term'),
];
$form['existing'] = [
'#type' => 'select',
'#title' => $this
->t('Existing term'),
'#empty_option' => $this
->t('Select an existing term'),
'#options' => $this
->buildExistingTermsOptions(),
];
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['submit'] = [
'#button_type' => 'primary',
'#type' => 'submit',
'#value' => $this
->t('Submit'),
];
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
$new = !empty($form_state
->getValue('new'));
$existing = !empty($form_state
->getValue('existing'));
if ($new !== $existing) {
return;
}
$form_state
->setErrorByName('new', $this
->t('You must either select an existing term or enter a new term.'));
}
public function submitForm(array &$form, FormStateInterface $form_state) {
if (!empty($form_state
->getValue('new'))) {
$this
->getTempStore()
->set('target', $form_state
->getValue('new'));
}
if (!empty($form_state
->getValue('existing'))) {
$term = $this->termStorage
->load($form_state
->getValue('existing'));
$this
->getTempStore()
->set('target', $term);
}
$routeName = 'entity.taxonomy_vocabulary.merge_confirm';
$routeParameters['taxonomy_vocabulary'] = $this->vocabulary
->id();
$form_state
->setRedirect($routeName, $routeParameters);
}
private function buildExistingTermsOptions() {
$query = $this->termStorage
->getQuery();
$query
->condition('vid', $this->vocabulary
->id())
->condition('tid', $this
->getSelectedTermIds(), 'NOT IN');
$terms = $this->termStorage
->loadMultiple($query
->execute());
$options = [];
foreach ($terms as $term) {
$options[$term
->id()] = $term
->label();
}
return $options;
}
private function getSelectedTermIds() {
return (array) $this
->getTempStore()
->get('terms');
}
private function getTempStore() {
return $this->tempStoreFactory
->get('term_merge');
}
}