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\TermInterface;
use Drupal\taxonomy\VocabularyInterface;
use Drupal\term_merge\TermMergerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class MergeTermsConfirm extends FormBase {
protected $entityTypeManager;
protected $termStorage;
private $tempStoreFactory;
private $termMerger;
private $vocabulary;
public function __construct(EntityTypeManagerInterface $entityTypeManager, PrivateTempStoreFactory $tempStoreFactory, TermMergerInterface $termMerger) {
$this->entityTypeManager = $entityTypeManager;
$this->termStorage = $entityTypeManager
->getStorage('taxonomy_term');
$this->tempStoreFactory = $tempStoreFactory;
$this->termMerger = $termMerger;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'), $container
->get('tempstore.private'), $container
->get('term_merge.term_merger'));
}
public function getFormId() {
return 'taxonomy_merge_terms_confirm';
}
public function buildForm(array $form, FormStateInterface $form_state, VocabularyInterface $taxonomy_vocabulary = NULL) {
$this->vocabulary = $taxonomy_vocabulary;
$selectedTermIds = $this
->getSelectedTermIds();
if (empty($selectedTermIds)) {
$this
->messenger()
->addError($this
->t("You must submit at least one term."), 'error');
return $form;
}
$target = $this->tempStoreFactory
->get('term_merge')
->get('target');
if (!is_string($target) && !$target instanceof TermInterface) {
throw new \LogicException("Invalid target type. Should be string or implement TermInterface");
}
$arguments = [
'%termCount' => count($selectedTermIds),
'%termName' => is_string($target) ? $target : $target
->label(),
];
if (is_string($target)) {
$form['message']['#markup'] = $this
->t("You are about to merge %termCount terms into new term %termName. This action can't be undone. Are you sure you wish to continue with merging the terms below?", $arguments);
}
else {
$form['message']['#markup'] = $this
->t("You are about to merge %termCount terms into existing term %termName. This action can't be undone. Are you sure you wish to continue with merging the terms below?", $arguments);
}
$form['terms'] = [
'#title' => $this
->t("Terms to be merged"),
'#theme' => 'item_list',
'#items' => $this
->getSelectedTermLabels(),
];
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['submit'] = [
'#button_type' => 'primary',
'#type' => 'submit',
'#value' => $this
->t('Confirm merge'),
];
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$selectedTerms = $this
->loadSelectedTerms();
$target = $this->tempStoreFactory
->get('term_merge')
->get('target');
if (is_string($target)) {
$this->termMerger
->mergeIntoNewTerm($selectedTerms, $target);
$this
->setSuccessfullyMergedMessage(count($selectedTerms), $target);
$this
->redirectToTermMergeForm($form_state);
return;
}
$this->termMerger
->mergeIntoTerm($selectedTerms, $target);
$this
->setSuccessfullyMergedMessage(count($selectedTerms), $target
->label());
$this
->redirectToTermMergeForm($form_state);
}
public function titleCallback() {
$termCount = count($this
->getSelectedTermIds());
$arguments = [
'%termCount' => $termCount,
];
return $this
->t("Are you sure you wish to merge %termCount terms?", $arguments);
}
private function getSelectedTermIds() {
$selectedTerms = $this->tempStoreFactory
->get('term_merge')
->get('terms');
if ($selectedTerms === NULL) {
$selectedTerms = [];
}
return $selectedTerms;
}
private function getSelectedTermLabels() {
$selectedTerms = $this
->loadSelectedTerms();
$items = [];
foreach ($selectedTerms as $term) {
$items[] = $term
->label();
}
return $items;
}
private function loadSelectedTerms() {
$termStorage = $this->entityTypeManager
->getStorage('taxonomy_term');
$selectedTerms = $termStorage
->loadMultiple($this
->getSelectedTermIds());
return $selectedTerms;
}
private function redirectToTermMergeForm(FormStateInterface $formState) {
$parameters['taxonomy_vocabulary'] = $this->vocabulary
->id();
$routeName = 'entity.taxonomy_vocabulary.merge_form';
$formState
->setRedirect($routeName, $parameters);
}
private function setSuccessfullyMergedMessage($count, $targetName) {
$arguments = [
'%count' => $count,
'%target' => $targetName,
];
$this
->messenger()
->addStatus($this
->t('Successfully merged %count terms into %target', $arguments));
}
}