3 namespace Drupal\config_translation\FormElement;
5 use Drupal\Core\Config\Config;
6 use Drupal\Core\Language\LanguageInterface;
7 use Drupal\config_translation\Form\ConfigTranslationFormBase;
8 use Drupal\Core\StringTranslation\StringTranslationTrait;
9 use Drupal\Core\TypedData\DataDefinitionInterface;
10 use Drupal\Core\TypedData\TraversableTypedDataInterface;
11 use Drupal\Core\TypedData\TypedDataInterface;
12 use Drupal\language\Config\LanguageConfigOverride;
15 * Defines the list element for the configuration translation interface.
17 class ListElement implements ElementInterface {
19 use StringTranslationTrait;
22 * The schema element this form is for.
24 * @var \Drupal\Core\TypedData\TraversableTypedDataInterface
29 * Constructs a ListElement.
31 * @param \Drupal\Core\TypedData\TraversableTypedDataInterface $element
32 * The schema element this form element is for.
34 public function __construct(TraversableTypedDataInterface $element) {
35 $this->element = $element;
41 public static function create(TypedDataInterface $schema) {
42 return new static($schema);
48 public function getTranslationBuild(LanguageInterface $source_language, LanguageInterface $translation_language, $source_config, $translation_config, array $parents, $base_key = NULL) {
50 foreach ($this->element as $key => $element) {
52 $element_key = isset($base_key) ? "$base_key.$key" : $key;
53 $definition = $element->getDataDefinition();
55 if ($form_element = ConfigTranslationFormBase::createFormElement($element)) {
56 $element_parents = array_merge($parents, [$key]);
57 $sub_build += $form_element->getTranslationBuild($source_language, $translation_language, $source_config[$key], $translation_config[$key], $element_parents, $element_key);
59 if (empty($sub_build)) {
63 // Build the sub-structure and include it with a wrapper in the form if
64 // there are any translatable elements there.
66 if ($element instanceof TraversableTypedDataInterface) {
69 '#title' => $this->getGroupTitle($definition, $sub_build),
70 '#open' => empty($base_key),
73 $build[$key] += $sub_build;
83 public function setConfig(Config $base_config, LanguageConfigOverride $config_translation, $config_values, $base_key = NULL) {
84 foreach ($this->element as $key => $element) {
85 $element_key = isset($base_key) ? "$base_key.$key" : $key;
86 if ($form_element = ConfigTranslationFormBase::createFormElement($element)) {
87 // Traverse into the next level of the configuration.
88 $value = isset($config_values[$key]) ? $config_values[$key] : NULL;
89 $form_element->setConfig($base_config, $config_translation, $value, $element_key);
95 * Returns the title for the 'details' element of a group of schema elements.
97 * For some configuration elements the same element structure can be repeated
98 * multiple times (for example views displays, filters, etc.). Thus, we try to
99 * find a more usable title for the details summary. First check if there is
100 * an element which is called title or label and use its value. Then check if
101 * there is an element which contains these words and use those. Fall back
102 * to the generic definition label if no such element is found.
104 * @param \Drupal\Core\TypedData\DataDefinitionInterface $definition
105 * The definition of the schema element.
106 * @param array $group_build
107 * The renderable array for the group of schema elements.
110 * The title for the group of schema elements.
112 protected function getGroupTitle(DataDefinitionInterface $definition, array $group_build) {
114 if (isset($group_build['title']['source'])) {
115 $title = $group_build['title']['source']['#markup'];
117 elseif (isset($group_build['label']['source'])) {
118 $title = $group_build['label']['source']['#markup'];
121 foreach (array_keys($group_build) as $title_key) {
122 if (isset($group_build[$title_key]['source']) && (strpos($title_key, 'title') !== FALSE || strpos($title_key, 'label') !== FALSE)) {
123 $title = $group_build[$title_key]['source']['#markup'];
128 return (!empty($title) ? (strip_tags($title) . ' ') : '') . $this->t($definition['label']);