Same filename and directory in other branches
- 8.9.x core/modules/config_translation/src/Form/ConfigTranslationFormBase.php
- 9 core/modules/config_translation/src/Form/ConfigTranslationFormBase.php
Namespace
Drupal\config_translation\Form
File
core/modules/config_translation/src/Form/ConfigTranslationFormBase.php
View source
<?php
namespace Drupal\config_translation\Form;
use Drupal\config_translation\ConfigMapperManagerInterface;
use Drupal\Core\Config\TypedConfigManagerInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\TypedData\TypedDataInterface;
use Drupal\Core\Form\BaseFormIdInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\language\ConfigurableLanguageManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
abstract class ConfigTranslationFormBase extends FormBase implements BaseFormIdInterface {
protected $typedConfigManager;
protected $configMapperManager;
protected $mapper;
protected $languageManager;
protected $language;
protected $sourceLanguage;
protected $baseConfigData = [];
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;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.typed'), $container
->get('plugin.manager.config_translation.mapper'), $container
->get('language_manager'));
}
public function getBaseFormId() {
return 'config_translation_form';
}
public function buildForm(array $form, FormStateInterface $form_state, RouteMatchInterface $route_match = NULL, $plugin_id = NULL, $langcode = NULL) {
$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;
$langcode = $this->mapper
->getLangcode();
$this->sourceLanguage = $this->languageManager
->getLanguage($langcode);
$this->baseConfigData = $this->mapper
->getConfigData();
$original_language = $this->languageManager
->getConfigOverrideLanguage();
$this->languageManager
->setConfigOverrideLanguage($this->language);
$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';
$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',
];
$this->languageManager
->setConfigOverrideLanguage($original_language);
return $form;
}
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);
$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);
$saved_config = $config_translation
->get();
if (empty($saved_config)) {
$config_translation
->delete();
$this
->messenger()
->addStatus($this
->t('@language translation was not added. To add a translation, you must modify the configuration.', [
'@language' => $this->language
->getName(),
]));
}
else {
$config_translation
->save();
$this
->messenger()
->addStatus($this
->t('Successfully saved @language translation.', [
'@language' => $this->language
->getName(),
]));
}
}
$form_state
->setRedirect($this->mapper
->getOverviewRouteName(), $this->mapper
->getOverviewRouteParameters());
}
public static function createFormElement(TypedDataInterface $schema) {
$definition = $schema
->getDataDefinition();
if (isset($definition['form_element_class'])) {
if (!$definition
->getLabel()) {
$definition
->setLabel(new TranslatableMarkup('n/a'));
}
$class = $definition['form_element_class'];
return $class::create($schema);
}
}
}
Classes