class MergeTerms in Term Merge 8
Term merge form.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\term_merge\Form\MergeTerms
Expanded class hierarchy of MergeTerms
1 file declares its use of MergeTerms
- MergeTermsTest.php in tests/
src/ Kernel/ Form/ MergeTermsTest.php
1 string reference to 'MergeTerms'
File
- src/
Form/ MergeTerms.php, line 15
Namespace
Drupal\term_merge\FormView source
class MergeTerms extends FormBase {
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The term storage handler.
*
* @var \Drupal\taxonomy\TermStorageInterface
*/
protected $termStorage;
/**
* The vocabulary.
*
* @var \Drupal\taxonomy\VocabularyInterface
*/
private $vocabulary;
/**
* The private temporary storage factory.
*
* @var \Drupal\Core\TempStore\PrivateTempStoreFactory
*/
private $tempStoreFactory;
/**
* Constructs a MergeTerms object.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* The entity manager service.
* @param \Drupal\Core\TempStore\PrivateTempStoreFactory $tempStoreFactory
* The private temporary storage factory service.
*/
public function __construct(EntityTypeManagerInterface $entityTypeManager, PrivateTempStoreFactory $tempStoreFactory) {
$this->entityTypeManager = $entityTypeManager;
$this->termStorage = $entityTypeManager
->getStorage('taxonomy_term');
$this->tempStoreFactory = $tempStoreFactory;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'), $container
->get('tempstore.private'));
}
/**
* Returns a unique string identifying the form.
*
* @return string
* The unique string identifying the form.
*/
public function getFormId() {
return 'taxonomy_merge_terms';
}
/**
* {@inheritdoc}
*
* @SuppressWarnings(camelCase)
*/
public function buildForm(array $form, FormStateInterface $form_state, VocabularyInterface $taxonomy_vocabulary = NULL) {
$this->vocabulary = $taxonomy_vocabulary;
$form['terms'] = [
'#type' => 'select',
'#title' => $this
->t('Terms to merge'),
'#options' => $this
->getTermOptions($taxonomy_vocabulary),
'#empty_option' => $this
->t('Select two or more terms to merge together'),
'#multiple' => TRUE,
'#required' => TRUE,
];
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['submit'] = [
'#button_type' => 'primary',
'#type' => 'submit',
'#value' => $this
->t('Merge'),
];
return $form;
}
/**
* {@inheritdoc}
*
* @SuppressWarnings(camelCase)
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
$selectedTerms = $form_state
->getValue('terms');
if (empty($selectedTerms)) {
$form_state
->setErrorByName('terms', 'At least one term must be selected.');
}
}
/**
* {@inheritdoc}
*
* @SuppressWarnings(camelCase)
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$selectedTerms = $form_state
->getValue('terms');
$termStore = $this->tempStoreFactory
->get('term_merge');
$termStore
->set('terms', $selectedTerms);
$routeName = 'entity.taxonomy_vocabulary.merge_target';
$routeParameters['taxonomy_vocabulary'] = $this->vocabulary
->id();
$form_state
->setRedirect($routeName, $routeParameters);
}
/**
* Callback for the form title.
*
* @param \Drupal\taxonomy\VocabularyInterface $taxonomy_vocabulary
* The vocabulary.
*
* @return \Drupal\Core\StringTranslation\TranslatableMarkup
* The title.
*
* @SuppressWarnings(camelCase)
*/
public function titleCallback(VocabularyInterface $taxonomy_vocabulary) {
return $this
->t('Merge %vocabulary terms', [
'%vocabulary' => $taxonomy_vocabulary
->label(),
]);
}
/**
* Builds a list of all terms in this vocabulary.
*
* @param \Drupal\taxonomy\VocabularyInterface $vocabulary
* The vocabulary.
*
* @return string[]
* An array of taxonomy term labels keyed by their id.
*/
private function getTermOptions(VocabularyInterface $vocabulary) {
$options = [];
$terms = $this->termStorage
->loadByProperties([
'vid' => $vocabulary
->id(),
]);
foreach ($terms as $term) {
$options[$term
->id()] = $term
->label();
}
return $options;
}
}
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. | |
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. | |
MergeTerms:: |
protected | property | The entity type manager. | |
MergeTerms:: |
private | property | The private temporary storage factory. | |
MergeTerms:: |
protected | property | The term storage handler. | |
MergeTerms:: |
private | property | The vocabulary. | |
MergeTerms:: |
public | function |
Plugin annotation
@SuppressWarnings(camelCase); Overrides FormInterface:: |
|
MergeTerms:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
MergeTerms:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
MergeTerms:: |
private | function | Builds a list of all terms in this vocabulary. | |
MergeTerms:: |
public | function |
Plugin annotation
@SuppressWarnings(camelCase); Overrides FormInterface:: |
|
MergeTerms:: |
public | function | Callback for the form title. | |
MergeTerms:: |
public | function |
Plugin annotation
@SuppressWarnings(camelCase); Overrides FormBase:: |
|
MergeTerms:: |
public | function | Constructs a MergeTerms 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. |