diff --git a/core/modules/config_translation/src/Form/ConfigTranslationFormBase.php b/core/modules/config_translation/src/Form/ConfigTranslationFormBase.php index 522493e..699742d 100644 --- a/core/modules/config_translation/src/Form/ConfigTranslationFormBase.php +++ b/core/modules/config_translation/src/Form/ConfigTranslationFormBase.php @@ -8,16 +8,12 @@ namespace Drupal\config_translation\Form; use Drupal\config_translation\ConfigMapperManagerInterface; -use Drupal\Core\Config\Config; use Drupal\Core\Config\Schema\ArrayElement; -use Drupal\Core\Config\Schema\Element; use Drupal\Core\Config\TypedConfigManagerInterface; use Drupal\Core\TypedData\TypedDataInterface; -use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Form\BaseFormIdInterface; use Drupal\Core\Form\FormBase; use Drupal\Core\Form\FormStateInterface; -use Drupal\language\Config\LanguageConfigOverride; use Drupal\language\ConfigurableLanguageManagerInterface; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\Request; @@ -204,11 +200,14 @@ public function submitForm(array &$form, FormStateInterface $form_state) { $config_factory->setOverrideState(FALSE); foreach ($this->mapper->getConfigNames() as $name) { + $schema = $this->typedConfigManager->get($name); + // Set configuration values based on form submission and source values. $base_config = $config_factory->get($name); $config_translation = $this->languageManager->getLanguageConfigOverride($this->language->id, $name); - $this->setConfig($base_config, $config_translation, $this->typedConfigManager->get($name), $form_values[$name]); + $element = $this->createFormElement($schema); + $element->setConfig($base_config, $config_translation, $form_values[$name]); // If no overrides, delete language specific configuration file. $saved_config = $config_translation->get(); @@ -256,7 +255,7 @@ protected function buildConfigForm($name, ArrayElement $schema, $source_config, $build = array(); if ($form_element = $this->createFormElement($schema)) { - $parents = array_merge(array('config_names', $name)); + $parents = array('config_names', $name); $key = ''; $build = $form_element->getElements($this->sourceLanguage, $this->language, $source_config, $translation_config, $key, $parents); } @@ -285,7 +284,7 @@ public static function createFormElement(TypedDataInterface $schema) { $definition = $schema->getDataDefinition(); if (!empty($definition['translatable']) || $schema instanceof ArrayElement) { if (!$definition->getLabel()) { - $definition->setLabel(t('N/A')); + $definition->setLabel(t('n/a')); } if (isset($definition['form_element_class'])) { /** @var \Drupal\config_translation\FormElement\ElementInterface $form_element */ @@ -294,63 +293,4 @@ public static function createFormElement(TypedDataInterface $schema) { } } - /** - * Sets configuration based on a nested form value array. - * - * @param \Drupal\Core\Config\Config $base_config - * Base configuration values, in the source language. - * @param \Drupal\language\Config\LanguageConfigOverride $config_translation - * Translation configuration override data. - * @param \Drupal\Core\Config\Schema\ArrayElement $schema - * Schema definition of configuration. - * @param array $config_values - * A simple one dimensional or recursive array: - * - simple: - * array(name => array('translation' => 'French site name')); - * - recursive: - * cancel_confirm => array( - * cancel_confirm.subject => array('translation' => 'Subject'), - * cancel_confirm.body => array('translation' => 'Body content'), - * ); - * Either format is used, the nested arrays are just containers and not - * needed for saving the data. - * @param string|null $base_key - * (optional) The base key that the schema and the configuration values - * belong to. This should be NULL for the top-level configuration object and - * be populated consecutively when recursing into the configuration - * structure. - * - * @return array - * Translation configuration override data. - */ - protected function setConfig(Config $base_config, LanguageConfigOverride $config_translation, ArrayElement $schema, array $config_values, $base_key = NULL) { - foreach ($schema as $key => $element) { - if (!isset($config_values[$key])) { - continue; - } - $value = $config_values[$key]; - - $element_key = implode('.', array_filter(array($base_key, $key))); - $definition = $element->getDataDefinition(); - // While the 'form_element_class' key is used for form building, the - // 'translatable' key is used for the setting of configuration values. - // This allows the form to contain values which will not be set, such as - // the TextFormat element's 'format' value. - if ($element instanceof Element && empty($definition['translatable'])) { - // Traverse into this level in the configuration. - $this->setConfig($base_config, $config_translation, $element, $value, $element_key); - } - else { - // Save value, if different from the source value in the base - // configuration. If same as original configuration, remove override. - if ($base_config->get($element_key) !== $value) { - $config_translation->set($element_key, $value); - } - else { - $config_translation->clear($element_key); - } - } - } - } - } diff --git a/core/modules/config_translation/src/FormElement/ElementInterface.php b/core/modules/config_translation/src/FormElement/ElementInterface.php index 3d629f6..4f3db25 100644 --- a/core/modules/config_translation/src/FormElement/ElementInterface.php +++ b/core/modules/config_translation/src/FormElement/ElementInterface.php @@ -7,8 +7,10 @@ namespace Drupal\config_translation\FormElement; +use Drupal\Core\Config\Config; use Drupal\Core\Language\LanguageInterface; use Drupal\Core\TypedData\DataDefinitionInterface; +use Drupal\language\Config\LanguageConfigOverride; /** * Provides an interface for configuration translation form elements. @@ -108,4 +110,33 @@ public function getSourceElement(LanguageInterface $source_language, $source_con */ public function getTranslationElement(LanguageInterface $language, $source_config, $translation_config); + /** + * Sets configuration based on a nested form value array. + * + * @param \Drupal\Core\Config\Config $base_config + * Base configuration values, in the source language. + * @param \Drupal\language\Config\LanguageConfigOverride $config_translation + * Translation configuration override data. + * @param array $config_values + * A simple one dimensional or recursive array: + * - simple: + * array(name => array('translation' => 'French site name')); + * - recursive: + * cancel_confirm => array( + * cancel_confirm.subject => array('translation' => 'Subject'), + * cancel_confirm.body => array('translation' => 'Body content'), + * ); + * Either format is used, the nested arrays are just containers and not + * needed for saving the data. + * @param string|null $base_key + * (optional) The base key that the schema and the configuration values + * belong to. This should be NULL for the top-level configuration object and + * be populated consecutively when recursing into the configuration + * structure. + * + * @return array + * Translation configuration override data. + */ + public function setConfig(Config $base_config, LanguageConfigOverride $config_translation, array $config_values, $base_key = NULL); + } diff --git a/core/modules/config_translation/src/FormElement/FormElementBase.php b/core/modules/config_translation/src/FormElement/FormElementBase.php index 57e8c4c..86e4c0e 100644 --- a/core/modules/config_translation/src/FormElement/FormElementBase.php +++ b/core/modules/config_translation/src/FormElement/FormElementBase.php @@ -7,10 +7,13 @@ namespace Drupal\config_translation\FormElement; +use Drupal\Core\Config\Config; +use Drupal\Core\Config\Schema\Element; use Drupal\Core\Language\LanguageInterface; use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\Core\TypedData\DataDefinitionInterface; use Drupal\Core\TypedData\TypedDataInterface; +use Drupal\language\Config\LanguageConfigOverride; /** * Provides a common base class for form elements. @@ -29,7 +32,7 @@ /** * Constructs a FormElementBase. * - * @param \Drupal\Core\TypedData\DataDefinitionInterface $definition + * @param TypedDataInterface $element */ public function __construct(TypedDataInterface $element) { $this->element = $element; @@ -95,4 +98,37 @@ public function getTranslationElement(LanguageInterface $translation_language, $ ); } + /** + * {@inheritdoc} + */ + public function setConfig(Config $base_config, LanguageConfigOverride $config_translation, array $config_values, $base_key = NULL) { + foreach ($this->element as $key => $element) { + if (!isset($config_values[$key])) { + continue; + } + $value = $config_values[$key]; + + $element_key = implode('.', array_filter(array($base_key, $key))); + $definition = $element->getDataDefinition(); + // While the 'form_element_class' key is used for form building, the + // 'translatable' key is used for the setting of configuration values. + // This allows the form to contain values which will not be set, such as + // the TextFormat element's 'format' value. + if ($element instanceof Element && empty($definition['translatable'])) { + // Traverse into this level in the configuration. + $this->setConfig($base_config, $config_translation, $value, $element_key); + } + else { + // Save value, if different from the source value in the base + // configuration. If same as original configuration, remove override. + if ($base_config->get($element_key) !== $value) { + $config_translation->set($element_key, $value); + } + else { + $config_translation->clear($element_key); + } + } + } + } + } diff --git a/core/modules/config_translation/src/FormElement/ListElement.php b/core/modules/config_translation/src/FormElement/ListElement.php index 2e6b3df..4230789 100644 --- a/core/modules/config_translation/src/FormElement/ListElement.php +++ b/core/modules/config_translation/src/FormElement/ListElement.php @@ -7,9 +7,12 @@ namespace Drupal\config_translation\FormElement; +use Drupal\Core\Config\Config; +use Drupal\Core\Config\Schema\Element; use Drupal\Core\Language\LanguageInterface; use Drupal\Core\Config\Schema\ArrayElement; use Drupal\config_translation\Form\ConfigTranslationFormBase; +use Drupal\language\Config\LanguageConfigOverride; /** * Defines the list element for the configuration translation interface.