diff --git a/core/modules/config_translation/src/Form/ConfigTranslationFormBase.php b/core/modules/config_translation/src/Form/ConfigTranslationFormBase.php index 585264c..72ba841 100644 --- a/core/modules/config_translation/src/Form/ConfigTranslationFormBase.php +++ b/core/modules/config_translation/src/Form/ConfigTranslationFormBase.php @@ -172,7 +172,15 @@ public function buildForm(array $form, FormStateInterface $form_state, Request $ $form['config_names'] = array('#type' => 'container'); foreach ($this->mapper->getConfigNames() as $name) { $form['config_names'][$name] = array('#type' => 'container'); - $form['config_names'][$name] += $this->buildConfigForm($name, $this->typedConfigManager->get($name), $this->baseConfigData[$name], $config_factory->get($name)->get()); + + $schema = $this->typedConfigManager->get($name); + $source_config = $this->baseConfigData[$name]; + $translation_config = $config_factory->get($name)->get(); + + if ($form_element = $this->createFormElement($schema)) { + $parents = array('config_names', $name); + $form['config_name'][$name] += $form_element->getTranslationBuild($this->sourceLanguage, $this->language, $source_config, $translation_config, $parents); + } } $form['actions']['#type'] = 'actions'; @@ -227,42 +235,6 @@ public function submitForm(array &$form, FormStateInterface $form_state) { } /** - * Formats configuration schema as a form tree. - * - * @param string $name - * The configuration name. - * @param \Drupal\Core\Config\Schema\ArrayElement $schema - * Schema definition of configuration. - * @param array|string $source_config - * Configuration object of base language, a string when done traversing - * the data building each sub-structure for the form. - * @param array|string $translation_config - * Configuration object of requested language, a string when done traversing - * the data building each sub-structure for the form. - * @param bool $open - * (optional) Whether or not the details element of the form should be open. - * Defaults to TRUE. - * @param string|null $base_key - * (optional) The base key to which the schema and configuration values - * belong. This should be NULL for the top-level configuration object and - * be populated consecutively when recursing into the configuration - * structure. - * - * @return array - * An associative array containing the structure of the form. - */ - protected function buildConfigForm($name, ArrayElement $schema, $source_config, $translation_config, $open = TRUE, $base_key = NULL) { - $build = array(); - - if ($form_element = $this->createFormElement($schema)) { - $parents = array('config_names', $name); - $build = $form_element->getElements($this->sourceLanguage, $this->language, $source_config, $translation_config, $parents); - } - - return $build; - } - - /** * Create form element builder. * * @param \Drupal\Core\TypedData\TypedDataInterface $schema @@ -280,7 +252,8 @@ public static function createFormElement(TypedDataInterface $schema) { if (!$definition->getLabel()) { $definition->setLabel(t('n/a')); } - return new $definition['form_element_class']($schema); + $class = $definition['form_element_class']; + return $class::create($schema); } } diff --git a/core/modules/config_translation/src/FormElement/ElementInterface.php b/core/modules/config_translation/src/FormElement/ElementInterface.php index d0df738..dd660b6 100644 --- a/core/modules/config_translation/src/FormElement/ElementInterface.php +++ b/core/modules/config_translation/src/FormElement/ElementInterface.php @@ -9,6 +9,7 @@ use Drupal\Core\Config\Config; use Drupal\Core\Language\LanguageInterface; +use Drupal\Core\TypedData\TypedDataInterface; use Drupal\language\Config\LanguageConfigOverride; /** @@ -17,6 +18,16 @@ interface ElementInterface { /** + * Creates a form element instance from a schema definition. + * + * @param \Drupal\Core\TypedData\TypedDataInterface $schema + * The configuration schema. + * + * @return static + */ + public static function create(TypedDataInterface $schema); + + /** * Returns the source and translation elements for a given configuration * definition. * @@ -37,7 +48,7 @@ * @return array * A render array for the source value. */ - public function getElements(LanguageInterface $source_language, LanguageInterface $translation_language, $source_config, $translation_config, $parents, $base_key = NULL); + public function getTranslationBuild(LanguageInterface $source_language, LanguageInterface $translation_language, $source_config, $translation_config, $parents, $base_key = NULL); /** * Sets configuration based on a nested form value array. diff --git a/core/modules/config_translation/src/FormElement/FormElementBase.php b/core/modules/config_translation/src/FormElement/FormElementBase.php index 9caa7f7..88d8f83 100644 --- a/core/modules/config_translation/src/FormElement/FormElementBase.php +++ b/core/modules/config_translation/src/FormElement/FormElementBase.php @@ -50,7 +50,14 @@ public function __construct(TypedDataInterface $element) { /** * {@inheritdoc} */ - public function getElements(LanguageInterface $source_language, LanguageInterface $translation_language, $source_config, $translation_config, $parents, $base_key = NULL) { + public static function create(TypedDataInterface $schema) { + return new static($schema); + } + + /** + * {@inheritdoc} + */ + public function getTranslationBuild(LanguageInterface $source_language, LanguageInterface $translation_language, $source_config, $translation_config, $parents, $base_key = NULL) { $build['#theme'] = 'config_translation_manage_form_element'; // For accessibility we make source and translation appear next to each diff --git a/core/modules/config_translation/src/FormElement/ListElement.php b/core/modules/config_translation/src/FormElement/ListElement.php index 18c0687..7867377 100644 --- a/core/modules/config_translation/src/FormElement/ListElement.php +++ b/core/modules/config_translation/src/FormElement/ListElement.php @@ -13,6 +13,7 @@ use Drupal\config_translation\Form\ConfigTranslationFormBase; use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\Core\TypedData\DataDefinitionInterface; +use Drupal\Core\TypedData\TypedDataInterface; use Drupal\language\Config\LanguageConfigOverride; /** @@ -30,13 +31,6 @@ class ListElement implements ElementInterface { protected $element; /** - * The data definition of the element this form element is for. - * - * @var \Drupal\Core\TypedData\ListDataDefinitionInterface - */ - protected $definition; - - /** * Constructs a FormElementBase. * * @param \Drupal\Core\Config\Schema\ArrayElement $element @@ -44,13 +38,19 @@ class ListElement implements ElementInterface { */ public function __construct(ArrayElement $element) { $this->element = $element; - $this->definition = $element->getDataDefinition(); } /** * {@inheritdoc} */ - public function getElements(LanguageInterface $source_language, LanguageInterface $translation_language, $source_config, $translation_config, $parents, $base_key = NULL) { + public static function create(TypedDataInterface $schema) { + return new static($schema); + } + + /** + * {@inheritdoc} + */ + public function getTranslationBuild(LanguageInterface $source_language, LanguageInterface $translation_language, $source_config, $translation_config, $parents, $base_key = NULL) { $build = array(); foreach ($this->element as $key => $element) { $sub_build = array(); @@ -77,7 +77,7 @@ public function getElements(LanguageInterface $source_language, LanguageInterfac // Therefore we make the 'source' and 'translation' keys the top-level // keys in $form_state['values']. $element_parents = array_merge($parents, array($key)); - $sub_build += $form_element->getElements($source_language, $translation_language, $source_config[$key], $translation_config[$key], $element_parents, $element_key); + $sub_build += $form_element->getTranslationBuild($source_language, $translation_language, $source_config[$key], $translation_config[$key], $element_parents, $element_key); if (empty($sub_build)) { continue;