diff --git a/core/lib/Drupal/Core/Config/Schema/core.data_types.schema.yml b/core/lib/Drupal/Core/Config/Schema/core.data_types.schema.yml index a0aeff3..49138f5 100644 --- a/core/lib/Drupal/Core/Config/Schema/core.data_types.schema.yml +++ b/core/lib/Drupal/Core/Config/Schema/core.data_types.schema.yml @@ -167,6 +167,6 @@ text_with_format: value: type: text label: 'Content' - fomrat: + format: type: string label: 'Text format' diff --git a/core/modules/config_translation/lib/Drupal/config_translation/Form/ConfigTranslationFormBase.php b/core/modules/config_translation/lib/Drupal/config_translation/Form/ConfigTranslationFormBase.php index c5dae18..5fca7b1 100644 --- a/core/modules/config_translation/lib/Drupal/config_translation/Form/ConfigTranslationFormBase.php +++ b/core/modules/config_translation/lib/Drupal/config_translation/Form/ConfigTranslationFormBase.php @@ -230,7 +230,7 @@ public function submitForm(array &$form, array &$form_state) { $config_translation = $this->languageManager->getLanguageConfigOverride($this->language->id, $name); $locations = $this->localeStorage->getLocations(array('type' => 'configuration', 'name' => $name)); - $this->setConfig($this->language, $base_config, $config_translation, $form_values[$name], !empty($locations)); + $this->setConfig($this->language, $base_config, $config_translation, $form_values[$name], $this->typedConfigManager->get($name), !empty($locations)); // If no overrides, delete language specific configuration file. $saved_config = $config_translation->get(); @@ -363,6 +363,8 @@ protected function buildConfigForm(Element $schema, $config_data, $base_config_d * ); * Either format is used, the nested arrays are just containers and not * needed for saving the data. + * @param \Drupal\Core\Config\Schema\Element $schema + * Schema definition of configuration. * @param bool $shipped_config * (optional) Flag to specify whether the configuration had a shipped * version and therefore should also be stored in the locale database. @@ -370,14 +372,13 @@ protected function buildConfigForm(Element $schema, $config_data, $base_config_d * @return array * Translation configuration override data. */ - protected function setConfig(Language $language, Config $base_config, Config $config_translation, array $config_values, $shipped_config = FALSE) { + protected function setConfig(Language $language, Config $base_config, Config $config_translation, array $config_values, Element $schema, $shipped_config = FALSE) { foreach ($config_values as $key => $value) { if (is_array($value) && !isset($value['translation'])) { // Traverse into this level in the configuration. - $this->setConfig($language, $base_config, $config_translation, $value, $shipped_config); + $this->setConfig($language, $base_config, $config_translation, $value, $schema, $shipped_config); } else { - // If the configuration file being translated was originally shipped, we // should update the locale translation storage. The string should // already be there, but we make sure to check. @@ -392,10 +393,43 @@ protected function setConfig(Language $language, Config $base_config, Config $co // If we got a translation, take that, otherwise create a new one. $translation = reset($translations) ?: $this->localeStorage->createTranslation($conditions); + // Some schema types provide a form_element_class which is responsible + // for providing us with a translation string. We iterate through the + // $key string to check if we have a schema definition for that. + $types = explode(".", $key); + krsort($types); + foreach ($types as $type) { + // If there is a schema definition, run the + // hook_config_translation_type_info_alter. + if (isset($schema[$type])) { + $element = $schema[$type]; + $definition = $element->getDataDefinition(); + + // Invoke hook_config_translation_type_info_alter() implementations to + // alter the configuration types. + $definitions = array( + $definition['type'] => &$definition, + ); + $this->moduleHandler->alter('config_translation_type_info', $definitions); + break; + } + } + + // If form_element_class is set, create a new object of this class and + // let it return the string of the translation. + if (isset($definition['form_element_class'])) { + /** @var \Drupal\config_translation\FormElement\ElementInterface $form_element */ + $form_element = new $definition['form_element_class'](); + + $translation_string = $form_element->getTranslationString($value['translation']); + } else { + $translation_string = $value['translation']; + } + // If we have a new translation or different from what is stored in // locale before, save this as an updated customize translation. - if ($translation->isNew() || $translation->getString() != $value['translation']) { - $translation->setString($value['translation']) + if ($translation->isNew() || $translation->getString() != $translation_string) { + $translation->setString($translation_string) ->setCustomized() ->save(); } diff --git a/core/modules/config_translation/lib/Drupal/config_translation/FormElement/Element.php b/core/modules/config_translation/lib/Drupal/config_translation/FormElement/Element.php index 8d8f0cc..ce33fcb 100644 --- a/core/modules/config_translation/lib/Drupal/config_translation/FormElement/Element.php +++ b/core/modules/config_translation/lib/Drupal/config_translation/FormElement/Element.php @@ -50,4 +50,11 @@ public function getRenderedSource($base_config_data, $key, Language $source_lang return $base_config_data[$key] ? ('' . nl2br($base_config_data[$key] . '')) : t('(Empty)'); } + /** + * {@inheritdoc} + */ + public function getTranslationString($value) { + return $value; + } + } diff --git a/core/modules/config_translation/lib/Drupal/config_translation/FormElement/ElementInterface.php b/core/modules/config_translation/lib/Drupal/config_translation/FormElement/ElementInterface.php index 96b84be..3713024 100644 --- a/core/modules/config_translation/lib/Drupal/config_translation/FormElement/ElementInterface.php +++ b/core/modules/config_translation/lib/Drupal/config_translation/FormElement/ElementInterface.php @@ -47,4 +47,16 @@ public function getFormElement(array $definition, Language $language, $value); */ public function getRenderedSource($base_config_data, $key, Language $source_language, Language $language); + + /** + * Returns the translation to be used in string storage. + * + * @param mixed $value + * The value of the translation element. + * + * @return string + * String of translation. + */ + public function getTranslationString($value); + } diff --git a/core/modules/config_translation/lib/Drupal/config_translation/FormElement/TextFormat.php b/core/modules/config_translation/lib/Drupal/config_translation/FormElement/TextFormat.php index a3b7657..522dbda 100644 --- a/core/modules/config_translation/lib/Drupal/config_translation/FormElement/TextFormat.php +++ b/core/modules/config_translation/lib/Drupal/config_translation/FormElement/TextFormat.php @@ -36,4 +36,11 @@ public function getRenderedSource($base_config_data, $key, Language $source_lang return $value ? '' . $value . '' : t('(Empty)'); } + /** + * {@inheritdoc} + */ + public function getTranslationString($value) { + return $value['value']; + } + } diff --git a/core/modules/config_translation/lib/Drupal/config_translation/Tests/ConfigTranslationFormTest.php b/core/modules/config_translation/lib/Drupal/config_translation/Tests/ConfigTranslationFormTest.php index c5bce3f..591c796 100644 --- a/core/modules/config_translation/lib/Drupal/config_translation/Tests/ConfigTranslationFormTest.php +++ b/core/modules/config_translation/lib/Drupal/config_translation/Tests/ConfigTranslationFormTest.php @@ -20,7 +20,7 @@ class ConfigTranslationFormTest extends WebTestBase { * * @var array */ - public static $modules = array('config_translation', 'config_translation_test'); + public static $modules = array('config_translation', 'config_translation_test', 'editor'); /** * The plugin ID of the mapper to test. diff --git a/core/modules/config_translation/lib/Drupal/config_translation/Tests/ConfigTranslationUiTest.php b/core/modules/config_translation/lib/Drupal/config_translation/Tests/ConfigTranslationUiTest.php index 788bf76..d32e894 100644 --- a/core/modules/config_translation/lib/Drupal/config_translation/Tests/ConfigTranslationUiTest.php +++ b/core/modules/config_translation/lib/Drupal/config_translation/Tests/ConfigTranslationUiTest.php @@ -704,7 +704,6 @@ public function testTextFormatTranslation() { $translation_page_url = "$translation_base_url/fr/add"; $this->assertLinkByHref($translation_page_url); - // Make sure original text is present on this page. $this->drupalGet($translation_page_url); // Update translatable fields. @@ -716,7 +715,7 @@ public function testTextFormatTranslation() { $this->drupalPostForm($translation_page_url, $edit, t('Save translation')); // Get translation and check we've got the right value. - $config_parsed = $file_storage->read('locale.config.fr.config_translation_test.content'); + $config_parsed = $file_storage->read('language.config.fr.config_translation_test.content'); $expected = array( 'content' => array( 'value' => 'Hello World - FR', diff --git a/core/modules/config_translation/tests/modules/config_translation_test/config/schema/config_translation_test.schema.yml b/core/modules/config_translation/tests/modules/config_translation_test/config/schema/config_translation_test.schema.yml index 369db53..cea0fa9 100644 --- a/core/modules/config_translation/tests/modules/config_translation_test/config/schema/config_translation_test.schema.yml +++ b/core/modules/config_translation/tests/modules/config_translation_test/config/schema/config_translation_test.schema.yml @@ -4,6 +4,15 @@ config_translation_test.content: type: mapping label: 'Content' mapping: + id: + type: string + label: 'Category identifier' + label: + type: label + label: 'Label' + langcode: + type: string + label: 'Default language' content: type: text_with_format - label: 'Test element: Text with format' + label: 'Content' \ No newline at end of file