abstract class ConfigTranslationFormBase in Drupal 8
Same name and namespace in other branches
- 9 core/modules/config_translation/src/Form/ConfigTranslationFormBase.php \Drupal\config_translation\Form\ConfigTranslationFormBase
- 10 core/modules/config_translation/src/Form/ConfigTranslationFormBase.php \Drupal\config_translation\Form\ConfigTranslationFormBase
Provides a base form for configuration translations.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\config_translation\Form\ConfigTranslationFormBase implements BaseFormIdInterface
Expanded class hierarchy of ConfigTranslationFormBase
1 file declares its use of ConfigTranslationFormBase
- ListElement.php in core/
modules/ config_translation/ src/ FormElement/ ListElement.php
File
- core/
modules/ config_translation/ src/ Form/ ConfigTranslationFormBase.php, line 19
Namespace
Drupal\config_translation\FormView source
abstract class ConfigTranslationFormBase extends FormBase implements BaseFormIdInterface {
/**
* The typed configuration manager.
*
* @var \Drupal\Core\Config\TypedConfigManagerInterface
*/
protected $typedConfigManager;
/**
* The configuration mapper manager.
*
* @var \Drupal\config_translation\ConfigMapperManagerInterface
*/
protected $configMapperManager;
/**
* The mapper for configuration translation.
*
* @var \Drupal\config_translation\ConfigMapperInterface
*/
protected $mapper;
/**
* The language manager.
*
* @var \Drupal\language\ConfigurableLanguageManagerInterface
*/
protected $languageManager;
/**
* The language of the configuration translation.
*
* @var \Drupal\Core\Language\LanguageInterface
*/
protected $language;
/**
* The language of the configuration translation source.
*
* @var \Drupal\Core\Language\LanguageInterface
*/
protected $sourceLanguage;
/**
* An array of base language configuration data keyed by configuration names.
*
* @var array
*/
protected $baseConfigData = [];
/**
* Constructs a ConfigTranslationFormBase.
*
* @param \Drupal\Core\Config\TypedConfigManagerInterface $typed_config_manager
* The typed configuration manager.
* @param \Drupal\config_translation\ConfigMapperManagerInterface $config_mapper_manager
* The configuration mapper manager.
* @param \Drupal\language\ConfigurableLanguageManagerInterface $language_manager
* The configurable language manager.
*/
public function __construct(TypedConfigManagerInterface $typed_config_manager, ConfigMapperManagerInterface $config_mapper_manager, ConfigurableLanguageManagerInterface $language_manager) {
$this->typedConfigManager = $typed_config_manager;
$this->configMapperManager = $config_mapper_manager;
$this->languageManager = $language_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('config.typed'), $container
->get('plugin.manager.config_translation.mapper'), $container
->get('language_manager'));
}
/**
* {@inheritdoc}
*/
public function getBaseFormId() {
return 'config_translation_form';
}
/**
* Implements \Drupal\Core\Form\FormInterface::buildForm().
*
* Builds configuration form with metadata and values from the source
* language.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* (optional) The route match.
* @param string $plugin_id
* (optional) The plugin ID of the mapper.
* @param string $langcode
* (optional) The language code of the language the form is adding or
* editing.
*
* @return array
* The form structure.
*
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
* Throws an exception if the language code provided as a query parameter in
* the request does not match an active language.
*/
public function buildForm(array $form, FormStateInterface $form_state, RouteMatchInterface $route_match = NULL, $plugin_id = NULL, $langcode = NULL) {
/** @var \Drupal\config_translation\ConfigMapperInterface $mapper */
$mapper = $this->configMapperManager
->createInstance($plugin_id);
$mapper
->populateFromRouteMatch($route_match);
$language = $this->languageManager
->getLanguage($langcode);
if (!$language) {
throw new NotFoundHttpException();
}
$this->mapper = $mapper;
$this->language = $language;
// ConfigTranslationFormAccess will not grant access if this raises an
// exception, so we can call this without a try-catch block here.
$langcode = $this->mapper
->getLangcode();
$this->sourceLanguage = $this->languageManager
->getLanguage($langcode);
// Get base language configuration to display in the form before setting the
// language to use for the form. This avoids repetitively settings and
// resetting the language to get original values later.
$this->baseConfigData = $this->mapper
->getConfigData();
// Set the translation target language on the configuration factory.
$original_language = $this->languageManager
->getConfigOverrideLanguage();
$this->languageManager
->setConfigOverrideLanguage($this->language);
// Add some information to the form state for easier form altering.
$form_state
->set('config_translation_mapper', $this->mapper);
$form_state
->set('config_translation_language', $this->language);
$form_state
->set('config_translation_source_language', $this->sourceLanguage);
$form['#attached']['library'][] = 'config_translation/drupal.config_translation.admin';
// Even though this is a nested form, we do not set #tree to TRUE because
// the form value structure is generated by using #parents for each element.
// @see \Drupal\config_translation\FormElement\FormElementBase::getElements()
$form['config_names'] = [
'#type' => 'container',
];
foreach ($this->mapper
->getConfigNames() as $name) {
$form['config_names'][$name] = [
'#type' => 'container',
];
$schema = $this->typedConfigManager
->get($name);
$source_config = $this->baseConfigData[$name];
$translation_config = $this
->configFactory()
->get($name)
->get();
if ($form_element = $this
->createFormElement($schema)) {
$parents = [
'config_names',
$name,
];
$form['config_names'][$name] += $form_element
->getTranslationBuild($this->sourceLanguage, $this->language, $source_config, $translation_config, $parents);
}
}
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Save translation'),
'#button_type' => 'primary',
];
// Set the configuration language back.
$this->languageManager
->setConfigOverrideLanguage($original_language);
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$form_values = $form_state
->getValue([
'translation',
'config_names',
]);
foreach ($form_values as $name => $value) {
$schema = $this->typedConfigManager
->get($name);
// Set configuration values based on form submission and source values.
$base_config = $this
->configFactory()
->getEditable($name);
$config_translation = $this->languageManager
->getLanguageConfigOverride($this->language
->getId(), $name);
$element = $this
->createFormElement($schema);
$element
->setConfig($base_config, $config_translation, $value);
// If no overrides, delete language specific configuration file.
$saved_config = $config_translation
->get();
if (empty($saved_config)) {
$config_translation
->delete();
}
else {
$config_translation
->save();
}
}
$form_state
->setRedirect($this->mapper
->getOverviewRoute(), $this->mapper
->getOverviewRouteParameters());
}
/**
* Creates a form element builder.
*
* @param \Drupal\Core\TypedData\TypedDataInterface $schema
* Schema definition of configuration.
*
* @return \Drupal\config_translation\FormElement\ElementInterface|null
* The element builder object if possible.
*/
public static function createFormElement(TypedDataInterface $schema) {
$definition = $schema
->getDataDefinition();
// Form element classes can be specified even for non-translatable elements
// such as the ListElement form element which is used for Mapping and
// Sequence schema elements.
if (isset($definition['form_element_class'])) {
if (!$definition
->getLabel()) {
$definition
->setLabel(t('n/a'));
}
$class = $definition['form_element_class'];
return $class::create($schema);
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ConfigTranslationFormBase:: |
protected | property | An array of base language configuration data keyed by configuration names. | |
ConfigTranslationFormBase:: |
protected | property | The configuration mapper manager. | |
ConfigTranslationFormBase:: |
protected | property | The language of the configuration translation. | |
ConfigTranslationFormBase:: |
protected | property | The language manager. | |
ConfigTranslationFormBase:: |
protected | property | The mapper for configuration translation. | |
ConfigTranslationFormBase:: |
protected | property | The language of the configuration translation source. | |
ConfigTranslationFormBase:: |
protected | property | The typed configuration manager. | |
ConfigTranslationFormBase:: |
public | function |
Implements \Drupal\Core\Form\FormInterface::buildForm(). Overrides FormInterface:: |
2 |
ConfigTranslationFormBase:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
ConfigTranslationFormBase:: |
public static | function | Creates a form element builder. | |
ConfigTranslationFormBase:: |
public | function |
Returns a string identifying the base form. Overrides BaseFormIdInterface:: |
|
ConfigTranslationFormBase:: |
public | function |
Form submission handler. Overrides FormInterface:: |
2 |
ConfigTranslationFormBase:: |
public | function | Constructs a ConfigTranslationFormBase. | |
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 |
FormInterface:: |
public | function | Returns a unique string identifying the form. | 236 |
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. | |
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. |