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 edf9f7c..f25fb52 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 @@ -327,28 +327,15 @@ protected function buildConfigForm($name, Element $schema, $config_data, $base_c /** @var \Drupal\config_translation\FormElement\ElementInterface $form_element */ $form_element = new $definition['form_element_class'](); - $value = $config_data[$key]; $build[$element_key] = array( '#theme' => 'config_translation_manage_form_element', ); - $build[$element_key]['source'] = array( - '#markup' => $form_element->getRenderedSource($base_config_data[$key], $this->sourceLanguage), - '#title' => $this->t( - '!label (!source_language)', - array( - '!label' => $this->t($definition['label']), - '!source_language' => $this->sourceLanguage->name, - ) - ), - '#type' => 'item', - // The 'item' element generally receives input, but in this case it is - // both unnecessary and unwanted, as we only want one form value per - // configuration element. - // @see system_element_info() - '#input' => FALSE, - ); + $build[$element_key]['source'] = $form_element->getSourceElement($definition, $this->sourceLanguage, $base_config_data[$key]); + // Make sure that the source does not end up in the form values. The + // 'item' element, for example, generally receives input, + $build[$element_key]['source']['#input'] = FALSE; - $build[$element_key]['translation'] = $form_element->getFormElement($definition, $this->language, $value); + $build[$element_key]['translation'] = $form_element->getTranslationElement($definition, $this->language, $config_data[$key]); // For accessibility we make source and translation appear next to each // other in the source for each element, which is why we utilize the // 'source' and 'translation' sub-keys for the form. The form values, diff --git a/core/modules/config_translation/lib/Drupal/config_translation/FormElement/DateFormat.php b/core/modules/config_translation/lib/Drupal/config_translation/FormElement/DateFormat.php index 497728d..5f0a29b 100644 --- a/core/modules/config_translation/lib/Drupal/config_translation/FormElement/DateFormat.php +++ b/core/modules/config_translation/lib/Drupal/config_translation/FormElement/DateFormat.php @@ -10,21 +10,17 @@ use Drupal\Component\Utility\NestedArray; use Drupal\Core\Ajax\AjaxResponse; use Drupal\Core\Ajax\ReplaceCommand; -use Drupal\Core\Language\Language; -use Drupal\Core\StringTranslation\StringTranslationTrait; +use Drupal\Core\Language\LanguageInterface; /** * Defines the date format element for the configuration translation interface. */ -class DateFormat implements ElementInterface { - - use SimpleSourceTrait; - use StringTranslationTrait; +class DateFormat extends FormElementBase { /** * {@inheritdoc} */ - public function getFormElement(array $definition, Language $language, $value) { + public function getTranslationElement(array $definition, LanguageInterface $language, $value) { if (class_exists('intlDateFormatter')) { $description = $this->t('A user-defined date format. See the PHP manual for available options.', array('@url' => 'http://userguide.icu-project.org/formatparse/datetime')); } @@ -32,19 +28,17 @@ public function getFormElement(array $definition, Language $language, $value) { $description = $this->t('A user-defined date format. See the PHP manual for available options.', array('@url' => 'http://php.net/manual/function.date.php')); } $format = $this->t('Displayed as %date_format', array('%date_format' => \Drupal::service('date')->format(REQUEST_TIME, 'custom', $value))); + return array( '#type' => 'textfield', - '#title' => $this->t($definition['label']) . ' (' . $language->name . ')', '#description' => $description, - '#default_value' => $value, - '#attributes' => array('lang' => $language->id), '#field_suffix' => '
' . $format . '
', '#ajax' => array( 'callback' => 'Drupal\config_translation\FormElement\DateFormat::ajaxSample', 'event' => 'keyup', 'progress' => array('type' => 'throbber', 'message' => NULL), ), - ); + ) + parent::getTranslationElement($definition, $language, $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 188d02d..07ddf47 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 @@ -7,7 +7,7 @@ namespace Drupal\config_translation\FormElement; -use Drupal\Core\Language\Language; +use Drupal\Core\Language\LanguageInterface; /** * Provides an interface for configuration translation form elements. @@ -18,28 +18,30 @@ * Returns the translation form element for a given configuration definition. * * @param array $definition - * Configuration schema type definition of the element. - * @param \Drupal\Core\Language\Language $language - * Language object to display the translation form for. + * The configuration schema for the element. + * @param \Drupal\Core\Language\LanguageInterface $language + * The language to display the translation form for. * @param string $value * Default value for the form element. * * @return array * Form API array to represent the form element. */ - public function getFormElement(array $definition, Language $language, $value); + public function getTranslationElement(array $definition, LanguageInterface $language, $value); /** - * Returns the rendered source element for a given configuration definition. + * Returns the source element for a given configuration definition. * + * @param array $definition + * The configuration schema for the element. + * @param \Drupal\Core\Language\LanguageInterface $source_language + * Thee source language of the configuration object. * @param array|string $base_config * The configuration value of the element in the source language. - * @param \Drupal\Core\Language\Language $source_language - * Thee source language of the configuration object. * - * @return string - * The rendered value in source language. + * @return array + * A render array for the source value. */ - public function getRenderedSource($base_config, Language $source_language); + public function getSourceElement(array $definition, LanguageInterface $source_language, $base_config); } diff --git a/core/modules/config_translation/lib/Drupal/config_translation/FormElement/FormElementBase.php b/core/modules/config_translation/lib/Drupal/config_translation/FormElement/FormElementBase.php new file mode 100644 index 0000000..6199024 --- /dev/null +++ b/core/modules/config_translation/lib/Drupal/config_translation/FormElement/FormElementBase.php @@ -0,0 +1,61 @@ + $this->t( + '!label (!source_language)', + array( + '!label' => $this->t($definition['label']), + '!source_language' => $language->getName(), + ) + ), '#default_value' => $value, + '#attributes' => array('lang' => $language->getId()), + ); + } + + /** + * {@inheritdoc} + */ + public function getSourceElement(array $definition, LanguageInterface $source_language, $base_config) { + if ($base_config) { + $value = '' . nl2br($base_config) . ''; + } + else { + $value = $this->t('(Empty)'); + } + + return array( + '#type' => 'item', + '#title' => $this->t( + '!label (!source_language)', + array( + '!label' => $this->t($definition['label']), + '!source_language' => $source_language->getName(), + ) + ), + '#markup' => $value, + ); + } + +} diff --git a/core/modules/config_translation/lib/Drupal/config_translation/FormElement/SimpleSourceTrait.php b/core/modules/config_translation/lib/Drupal/config_translation/FormElement/SimpleSourceTrait.php deleted file mode 100644 index 5d52567..0000000 --- a/core/modules/config_translation/lib/Drupal/config_translation/FormElement/SimpleSourceTrait.php +++ /dev/null @@ -1,34 +0,0 @@ -id . '">' . nl2br($base_config . '')) : t('(Empty)'); - } - -} 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 2b1e494..534b2dc 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 @@ -7,36 +7,36 @@ namespace Drupal\config_translation\FormElement; -use Drupal\Core\Language\Language; +use Drupal\Core\Language\LanguageInterface; use Drupal\Core\StringTranslation\StringTranslationTrait; /** * Defines the text_format element for the configuration translation interface. */ -class TextFormat implements ElementInterface { - - use StringTranslationTrait; +class TextFormat extends FormElementBase { /** * {@inheritdoc} */ - public function getFormElement(array $definition, Language $language, $value) { - $element = array( + public function getTranslationElement(array $definition, LanguageInterface $language, $value) { + // Override the #default_value property from the parent class. + return array( '#type' => 'text_format', '#default_value' => $value['value'], '#format' => $value['format'], - '#title' => $this->t($definition['label']) . ' (' . $language->name . ')', - '#attributes' => array('lang' => $language->id), - ); - return $element; + ) + parent::getTranslationElement($definition, $language, $value); } /** * {@inheritdoc} */ - public function getRenderedSource($base_config, Language $source_language) { - $value = check_markup($base_config['value'], $base_config['format'], $source_language->id); - return $value ? '' . $value . '' : t('(Empty)'); + public function getSourceElement(array $definition, LanguageInterface $source_language, $base_config) { + // Instead of the formatted output show a disabled textarea. This allows for + // easier side-by-side comparison, especially with for formats with text + // editors. + return $this->getTranslationElement($definition, $source_language, $base_config) + array( + '#disabled' => TRUE, + ); } } diff --git a/core/modules/config_translation/lib/Drupal/config_translation/FormElement/Textarea.php b/core/modules/config_translation/lib/Drupal/config_translation/FormElement/Textarea.php index 54110a1..d276249 100644 --- a/core/modules/config_translation/lib/Drupal/config_translation/FormElement/Textarea.php +++ b/core/modules/config_translation/lib/Drupal/config_translation/FormElement/Textarea.php @@ -7,21 +7,17 @@ namespace Drupal\config_translation\FormElement; -use Drupal\Core\Language\Language; -use Drupal\Core\StringTranslation\StringTranslationTrait; +use Drupal\Core\Language\LanguageInterface; /** * Defines the textarea element for the configuration translation interface. */ -class Textarea implements ElementInterface { - - use SimpleSourceTrait; - use StringTranslationTrait; +class Textarea extends FormElementBase { /** * {@inheritdoc} */ - public function getFormElement(array $definition, Language $language, $value) { + public function getTranslationElement(array $definition, LanguageInterface $language, $value) { // Estimate a comfortable size of the input textarea. $rows_words = ceil(str_word_count($value) / 5); $rows_newlines = substr_count($value, "\n" ) + 1; @@ -29,11 +25,8 @@ public function getFormElement(array $definition, Language $language, $value) { return array( '#type' => 'textarea', - '#default_value' => $value, - '#title' => $this->t($definition['label']) . ' (' . $language->name . ')', '#rows' => $rows, - '#attributes' => array('lang' => $language->id), - ); + ) + parent::getTranslationElement($definition, $language, $value); } } diff --git a/core/modules/config_translation/lib/Drupal/config_translation/FormElement/Textfield.php b/core/modules/config_translation/lib/Drupal/config_translation/FormElement/Textfield.php index 7edbfd3..687fdfb 100644 --- a/core/modules/config_translation/lib/Drupal/config_translation/FormElement/Textfield.php +++ b/core/modules/config_translation/lib/Drupal/config_translation/FormElement/Textfield.php @@ -7,27 +7,20 @@ namespace Drupal\config_translation\FormElement; -use Drupal\Core\Language\Language; -use Drupal\Core\StringTranslation\StringTranslationTrait; +use Drupal\Core\Language\LanguageInterface; /** * Defines the textfield element for the configuration translation interface. */ -class Textfield implements ElementInterface { - - use SimpleSourceTrait; - use StringTranslationTrait; +class Textfield extends FormElementBase { /** * {@inheritdoc} */ - public function getFormElement(array $definition, Language $language, $value) { + public function getTranslationElement(array $definition, LanguageInterface $language, $value) { return array( '#type' => 'textfield', - '#default_value' => $value, - '#title' => $this->t($definition['label']) . ' (' . $language->name . ')', - '#attributes' => array('lang' => $language->id), - ); + ) + parent::getTranslationElement($definition, $language, $value); } } diff --git a/core/modules/config_translation/tests/modules/config_translation_test/config_translation_test.info.yml b/core/modules/config_translation/tests/modules/config_translation_test/config_translation_test.info.yml index 47e5f1f..d7bf8ef 100644 --- a/core/modules/config_translation/tests/modules/config_translation_test/config_translation_test.info.yml +++ b/core/modules/config_translation/tests/modules/config_translation_test/config_translation_test.info.yml @@ -6,4 +6,5 @@ version: VERSION core: 8.x hidden: true dependencies: + - config_translation - config_test