class MergeTermsConfirm in Term Merge 8
Term merge confirm form.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\term_merge\Form\MergeTermsConfirm
Expanded class hierarchy of MergeTermsConfirm
1 file declares its use of MergeTermsConfirm
- MergeTermsConfirmTest.php in tests/
src/ Kernel/ Form/ MergeTermsConfirmTest.php
1 string reference to 'MergeTermsConfirm'
File
- src/
Form/ MergeTermsConfirm.php, line 17
Namespace
Drupal\term_merge\FormView source
class MergeTermsConfirm extends FormBase {
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The term storage handler.
*
* @var \Drupal\taxonomy\TermStorageInterface
*/
protected $termStorage;
/**
* The private temporary storage factory.
*
* @var \Drupal\Core\TempStore\PrivateTempStoreFactory
*/
private $tempStoreFactory;
/**
* The term merger.
*
* @var \Drupal\term_merge\TermMergerInterface
*/
private $termMerger;
/**
* The vocabulary.
*
* @var \Drupal\taxonomy\VocabularyInterface
*/
private $vocabulary;
/**
* Constructs a MergeTermsConfirm object.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* The entity manager service.
* @param \Drupal\Core\TempStore\PrivateTempStoreFactory $tempStoreFactory
* The private temporary storage factory.
* @param \Drupal\term_merge\TermMergerInterface $termMerger
* The term merger service.
*/
public function __construct(EntityTypeManagerInterface $entityTypeManager, PrivateTempStoreFactory $tempStoreFactory, TermMergerInterface $termMerger) {
$this->entityTypeManager = $entityTypeManager;
$this->termStorage = $entityTypeManager
->getStorage('taxonomy_term');
$this->tempStoreFactory = $tempStoreFactory;
$this->termMerger = $termMerger;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'), $container
->get('tempstore.private'), $container
->get('term_merge.term_merger'));
}
/**
* Returns a unique string identifying the form.
*
* @return string
* The unique string identifying the form.
*/
public function getFormId() {
return 'taxonomy_merge_terms_confirm';
}
/**
* {@inheritdoc}
*
* @SuppressWarnings(camelCase)
* @SuppressWarnings("else")
*/
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;
}
/**
* {@inheritdoc}
*
* @SuppressWarnings(camelCase)
*/
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);
}
/**
* Callback for the form title.
*
* @return \Drupal\Core\StringTranslation\TranslatableMarkup
* The title.
*
* @SuppressWarnings(camelCase)
*/
public function titleCallback() {
$termCount = count($this
->getSelectedTermIds());
$arguments = [
'%termCount' => $termCount,
];
return $this
->t("Are you sure you wish to merge %termCount terms?", $arguments);
}
/**
* Gets a list of selected term ids from the temp store.
*
* @return int[]
* The selected term ids.
*/
private function getSelectedTermIds() {
$selectedTerms = $this->tempStoreFactory
->get('term_merge')
->get('terms');
if ($selectedTerms === NULL) {
$selectedTerms = [];
}
return $selectedTerms;
}
/**
* Gets a list of selected term labels from the temp store.
*
* @return \Drupal\taxonomy\TermInterface[]
* The labels of the selected terms.
*/
private function getSelectedTermLabels() {
$selectedTerms = $this
->loadSelectedTerms();
$items = [];
foreach ($selectedTerms as $term) {
$items[] = $term
->label();
}
return $items;
}
/**
* Loads the selected terms.
*
* @return \Drupal\taxonomy\TermInterface[]
* The selected terms.
*/
private function loadSelectedTerms() {
$termStorage = $this->entityTypeManager
->getStorage('taxonomy_term');
/** @var \Drupal\taxonomy\TermInterface[] $selectedTerms */
$selectedTerms = $termStorage
->loadMultiple($this
->getSelectedTermIds());
return $selectedTerms;
}
/**
* Sets a redirect to the term merge form.
*
* @param \Drupal\Core\Form\FormStateInterface $formState
* The form state object to set the redirect on.
*/
private function redirectToTermMergeForm(FormStateInterface $formState) {
$parameters['taxonomy_vocabulary'] = $this->vocabulary
->id();
$routeName = 'entity.taxonomy_vocabulary.merge_form';
$formState
->setRedirect($routeName, $parameters);
}
/**
* Sets the successfully merged terms message.
*
* @param int $count
* The numner of terms merged.
* @param string $targetName
* The name of the target term.
*/
private function setSuccessfullyMergedMessage($count, $targetName) {
$arguments = [
'%count' => $count,
'%target' => $targetName,
];
$this
->messenger()
->addStatus($this
->t('Successfully merged %count terms into %target', $arguments));
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
FormBase:: |
public | function |
Form validation handler. Overrides FormInterface:: |
62 |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MergeTermsConfirm:: |
protected | property | The entity type manager. | |
MergeTermsConfirm:: |
private | property | The private temporary storage factory. | |
MergeTermsConfirm:: |
private | property | The term merger. | |
MergeTermsConfirm:: |
protected | property | The term storage handler. | |
MergeTermsConfirm:: |
private | property | The vocabulary. | |
MergeTermsConfirm:: |
public | function |
Plugin annotation
@SuppressWarnings(camelCase)
@SuppressWarnings("else") Overrides FormInterface:: |
|
MergeTermsConfirm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
MergeTermsConfirm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
MergeTermsConfirm:: |
private | function | Gets a list of selected term ids from the temp store. | |
MergeTermsConfirm:: |
private | function | Gets a list of selected term labels from the temp store. | |
MergeTermsConfirm:: |
private | function | Loads the selected terms. | |
MergeTermsConfirm:: |
private | function | Sets a redirect to the term merge form. | |
MergeTermsConfirm:: |
private | function | Sets the successfully merged terms message. | |
MergeTermsConfirm:: |
public | function |
Plugin annotation
@SuppressWarnings(camelCase); Overrides FormInterface:: |
|
MergeTermsConfirm:: |
public | function | Callback for the form title. | |
MergeTermsConfirm:: |
public | function | Constructs a MergeTermsConfirm object. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |