class TranslatorForm in Translation Management Tool 8
Form controller for the translator edit forms.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
- class \Drupal\tmgmt\Form\TranslatorForm
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
Expanded class hierarchy of TranslatorForm
Related topics
1 file declares its use of TranslatorForm
File
- src/
Form/ TranslatorForm.php, line 18
Namespace
Drupal\tmgmt\FormView source
class TranslatorForm extends EntityForm {
/**
* @var \Drupal\tmgmt\TranslatorInterface
*/
protected $entity;
/**
* Translator plugin manager.
*
* @var \Drupal\tmgmt\TranslatorManager
*/
protected $translatorManager;
/**
* Source plugin manager.
*
* @var \Drupal\tmgmt\SourceManager
*/
protected $sourceManager;
/**
* Constructs an EntityForm object.
*
* @param \Drupal\Core\Extension\ModuleHandlerInterface
* The module handler service.
* @param \Drupal\tmgmt\TranslatorManager $translator_manager
* The translator plugin manager.
*/
public function __construct(TranslatorManager $translator_manager, SourceManager $source_manager) {
$this->translatorManager = $translator_manager;
$this->sourceManager = $source_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('plugin.manager.tmgmt.translator'), $container
->get('plugin.manager.tmgmt.source'));
}
/**
* Overrides Drupal\Core\Entity\EntityForm::form().
*/
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
if ($this->operation == 'clone') {
$this->entity = $this->entity
->createDuplicate();
}
$entity = $this->entity;
// Check if the translator is currently in use.
if ($busy = !$entity
->isNew() ? tmgmt_translator_busy($entity
->id()) : FALSE) {
\Drupal::messenger()
->addWarning(t("This provider is currently in use. It cannot be deleted. The chosen provider Plugin cannot be changed."));
}
$available = $this->translatorManager
->getLabels();
// If the translator plugin is not set, pick the first available plugin as the
// default.
if (!$entity
->hasPlugin()) {
$entity
->setPluginID(key($available));
}
$form['label'] = array(
'#type' => 'textfield',
'#title' => t('Label'),
'#description' => t('The label of the provider.'),
'#default_value' => $entity
->label(),
'#required' => TRUE,
'#size' => 32,
'#maxlength' => 64,
);
$form['name'] = array(
'#type' => 'machine_name',
'#title' => t('Machine name'),
'#description' => t('The machine readable name of this provider. It must be unique, and it must contain only alphanumeric characters and underscores. Once created, you will not be able to change this value!'),
'#default_value' => $entity
->id(),
'#machine_name' => array(
'exists' => '\\Drupal\\tmgmt\\Entity\\Translator::load',
'source' => array(
'label',
),
),
'#disabled' => !$entity
->isNew(),
'#size' => 32,
'#maxlength' => 64,
);
$form['description'] = array(
'#type' => 'textarea',
'#title' => t('Description'),
'#description' => t('The description of the provider.'),
'#default_value' => $entity
->getDescription(),
'#size' => 32,
'#maxlength' => 255,
);
$form['auto_accept'] = array(
'#type' => 'checkbox',
'#title' => t('Auto accept finished translations'),
'#description' => t('This skips the reviewing process and automatically accepts all translations as soon as they are returned by the translation provider.'),
'#default_value' => $entity
->isAutoAccept(),
);
$form['plugin_wrapper'] = array(
'#type' => 'container',
'#prefix' => '<div id="tmgmt-plugin-wrapper">',
'#suffix' => '</div>',
);
// Pull the translator plugin info if any.
if ($entity
->hasPlugin()) {
$definition = $this->translatorManager
->getDefinition($entity
->getPluginId());
$form['plugin_wrapper']['plugin'] = array(
'#type' => 'select',
'#title' => t('Provider plugin'),
'#submit' => array(
'::updateRemoteLanguagesMappings',
),
'#limit_validation_errors' => array(
array(
'plugin',
),
),
'#executes_submit_callback' => TRUE,
'#description' => isset($definition['description']) ? Xss::filter($definition['description']) : '',
'#options' => $available,
'#default_value' => $entity
->getPluginId(),
'#required' => TRUE,
'#disabled' => $busy,
'#ajax' => array(
'callback' => array(
$this,
'ajaxTranslatorPluginSelect',
),
'wrapper' => 'tmgmt-plugin-wrapper',
),
);
// Add the provider logo in the settings wrapper.
if (isset($definition['logo'])) {
$form['plugin_wrapper']['logo'] = $logo_render_array = [
'#theme' => 'image',
'#uri' => file_create_url(drupal_get_path('module', $definition['provider']) . '/' . $definition['logo']),
'#alt' => $definition['label'],
'#title' => $definition['label'],
'#attributes' => [
'class' => 'tmgmt-logo-settings',
],
'#suffix' => '<div class="clearfix"></div>',
];
}
$form['plugin_wrapper']['settings'] = array(
'#type' => 'details',
'#title' => t('@plugin plugin settings', array(
'@plugin' => $definition['label'],
)),
'#tree' => TRUE,
'#open' => TRUE,
);
// Add the translator plugin settings form.
$plugin_ui = $this->translatorManager
->createUIInstance($entity
->getPluginId());
$form_state
->set('busy', $busy);
$form['plugin_wrapper']['settings'] += $plugin_ui
->buildConfigurationForm($form['plugin_wrapper']['settings'], $form_state);
if (!Element::children($form['plugin_wrapper']['settings'])) {
$form['#description'] = t("The @plugin plugin doesn't provide any settings.", array(
'@plugin' => $plugin_ui
->getPluginDefinition()['label'],
));
}
// If current translator is configured to provide remote language mapping
// provide the form to configure mappings, unless it does not exists yet.
if ($entity
->providesRemoteLanguageMappings()) {
$form['plugin_wrapper']['remote_languages_mappings'] = array(
'#tree' => TRUE,
'#type' => 'details',
'#title' => t('Remote languages mappings'),
'#description' => t('Here you can specify mappings of your local language codes to the translator language codes.'),
'#open' => TRUE,
);
$options = $entity
->getSupportedRemoteLanguages();
foreach ($entity
->getRemoteLanguagesMappings() as $local_language => $remote_language) {
$form['plugin_wrapper']['remote_languages_mappings'][$local_language] = array(
'#type' => 'textfield',
'#title' => \Drupal::languageManager()
->getLanguage($local_language)
->getName() . ' (' . $local_language . ')',
'#default_value' => $remote_language,
'#size' => 6,
);
if (!empty($options)) {
$form['plugin_wrapper']['remote_languages_mappings'][$local_language]['#type'] = 'select';
$form['plugin_wrapper']['remote_languages_mappings'][$local_language]['#options'] = $options;
$form['plugin_wrapper']['remote_languages_mappings'][$local_language]['#empty_option'] = ' - ';
unset($form['plugin_wrapper']['remote_languages_mappings'][$local_language]['#size']);
}
}
}
}
$form['#attached']['library'][] = 'tmgmt/admin';
return $form;
}
/**
* Updates remote languages mappings.
*
* @param array $form
* An associative array containing the initial structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the complete form.
*/
public static function updateRemoteLanguagesMappings(array $form, FormStateInterface $form_state) {
if (!empty($form_state
->getUserInput()['remote_languages_mappings'])) {
// The user input containing remote languages mappings from an old
// translator, so We have to remove them from here.
$user_input = $form_state
->getUserInput();
unset($user_input['remote_languages_mappings']);
$form_state
->setUserInput($user_input);
}
$form_state
->setRebuild();
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
if (!$form_state
->getValue('plugin')) {
$form_state
->setErrorByName('plugin', $this
->t('You have to select a translator plugin.'));
}
$plugin_ui = $this->translatorManager
->createUIInstance($this->entity
->getPluginID());
$plugin_ui
->validateConfigurationForm($form, $form_state);
}
/**
* Overrides Drupal\Core\Entity\EntityForm::save().
*/
public function save(array $form, FormStateInterface $form_state) {
$entity = $this->entity;
$status = $entity
->save();
if ($status === SAVED_UPDATED) {
\Drupal::messenger()
->addStatus($this
->t('%label configuration has been updated.', array(
'%label' => $entity
->label(),
)));
}
else {
\Drupal::messenger()
->addStatus($this
->t('%label configuration has been created.', array(
'%label' => $entity
->label(),
)));
}
$form_state
->setRedirect('entity.tmgmt_translator.collection');
}
/**
* {@inheritdoc}
*/
public function delete(array $form, FormStateInterface $form_state) {
$form_state
->setRedirectUrl($this->entity
->toUrl('delete-form'));
}
/**
* Ajax callback for loading the translator plugin settings form for the
* currently selected translator plugin.
*/
public static function ajaxTranslatorPluginSelect(array $form, FormStateInterface $form_state) {
return $form['plugin_wrapper'];
}
}
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 | |
EntityForm:: |
protected | property | The entity type manager. | 3 |
EntityForm:: |
protected | property | The module handler service. | |
EntityForm:: |
protected | property | The name of the current operation. | |
EntityForm:: |
private | property | The entity manager. | |
EntityForm:: |
protected | function | Returns an array of supported actions for the current entity form. | 29 |
EntityForm:: |
protected | function | Returns the action form element for the current entity form. | |
EntityForm:: |
public | function | Form element #after_build callback: Updates the entity with submitted data. | |
EntityForm:: |
public | function |
Builds an updated entity object based upon the submitted form values. Overrides EntityFormInterface:: |
2 |
EntityForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
10 |
EntityForm:: |
protected | function | Copies top-level form values to entity properties | 7 |
EntityForm:: |
public | function |
Returns a string identifying the base form. Overrides BaseFormIdInterface:: |
5 |
EntityForm:: |
public | function |
Gets the form entity. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Determines which entity will be used by this form from a RouteMatch object. Overrides EntityFormInterface:: |
1 |
EntityForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
10 |
EntityForm:: |
public | function |
Gets the operation identifying the form. Overrides EntityFormInterface:: |
|
EntityForm:: |
protected | function | Initialize the form state and the entity before the first form build. | 3 |
EntityForm:: |
protected | function | Prepares the entity object before the form is built first. | 3 |
EntityForm:: |
protected | function | Invokes the specified prepare hook variant. | |
EntityForm:: |
public | function | Process callback: assigns weights and hides extra fields. | |
EntityForm:: |
public | function |
Sets the form entity. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the entity manager for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the entity type manager for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the module handler for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the operation for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
This is the default entity object builder function. It is called before any
other submit handler to build the new entity object to be used by the
following submit handlers. At this point of the form workflow the entity is
validated and the form state… Overrides FormInterface:: |
17 |
EntityForm:: |
public | function | ||
EntityForm:: |
public | function | ||
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. | |
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. | |
TranslatorForm:: |
protected | property |
Overrides EntityForm:: |
|
TranslatorForm:: |
protected | property | Source plugin manager. | |
TranslatorForm:: |
protected | property | Translator plugin manager. | |
TranslatorForm:: |
public static | function | Ajax callback for loading the translator plugin settings form for the currently selected translator plugin. | |
TranslatorForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
TranslatorForm:: |
public | function | ||
TranslatorForm:: |
public | function |
Overrides Drupal\Core\Entity\EntityForm::form(). Overrides EntityForm:: |
|
TranslatorForm:: |
public | function |
Overrides Drupal\Core\Entity\EntityForm::save(). Overrides EntityForm:: |
|
TranslatorForm:: |
public static | function | Updates remote languages mappings. | |
TranslatorForm:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
TranslatorForm:: |
public | function | Constructs an EntityForm object. | |
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. |