From 88ff44b8c1259a76c854ae26adef9b529b42dbd6 Mon Sep 17 00:00:00 2001 From: tchap Date: Tue, 21 Dec 2021 15:21:09 +0100 Subject: [PATCH] Add translatable locale labels (Should fix #262) (#365) --- .../EventListener/TranslationsListener.php | 1 + src/Form/Type/TranslationsType.php | 1 + .../Type/TranslationsTypeAdvancedTest.php | 20 +++++++++++++++++++ 3 files changed, 22 insertions(+) diff --git a/src/Form/EventListener/TranslationsListener.php b/src/Form/EventListener/TranslationsListener.php index 7b9b653..2e0f068 100644 --- a/src/Form/EventListener/TranslationsListener.php +++ b/src/Form/EventListener/TranslationsListener.php @@ -57,6 +57,7 @@ public function preSetData(FormEvent $event): void $form->add($locale, AutoFormType::class, [ 'data_class' => $translationClass, + 'label' => $formOptions['locale_labels'][$locale] ?? null, 'required' => \in_array($locale, $formOptions['required_locales'], true), 'block_name' => ('field' === $formOptions['theming_granularity']) ? 'locale' : null, 'fields' => $fieldsOptions[$locale], diff --git a/src/Form/Type/TranslationsType.php b/src/Form/Type/TranslationsType.php index 2ebe026..88f30e2 100644 --- a/src/Form/Type/TranslationsType.php +++ b/src/Form/Type/TranslationsType.php @@ -53,6 +53,7 @@ public function configureOptions(OptionsResolver $resolver): void 'empty_data' => function (FormInterface $form) { return new ArrayCollection(); }, + 'locale_labels' => null, 'locales' => $this->localeProvider->getLocales(), 'default_locale' => $this->localeProvider->getDefaultLocale(), 'required_locales' => $this->localeProvider->getRequiredLocales(), diff --git a/tests/Form/Type/TranslationsTypeAdvancedTest.php b/tests/Form/Type/TranslationsTypeAdvancedTest.php index e290091..3bb2c9b 100644 --- a/tests/Form/Type/TranslationsTypeAdvancedTest.php +++ b/tests/Form/Type/TranslationsTypeAdvancedTest.php @@ -75,6 +75,26 @@ public function testEmptyFormOverrideFields(): void static::assertEquals(['title'], array_keys($translationsForm['de']->all()), 'Fields should not contains description'); } + public function testLabels(): void + { + $form = $this->factory->createBuilder(FormType::class, new Product()) + ->add('url') + ->add('translations', TranslationsType::class, [ + 'locale_labels' => [ + 'fr' => 'Français', + 'en' => 'English', + ], + ]) + ->add('save', SubmitType::class) + ->getForm() + ; + + $translationsForm = $form->get('translations')->all(); + static::assertEquals('English', $translationsForm['en']->getConfig()->getOptions()['label'], 'Label should be explicitely set'); + static::assertEquals('Français', $translationsForm['fr']->getConfig()->getOptions()['label'], 'Label should be explicitely set'); + static::assertNull($translationsForm['de']->getConfig()->getOptions()['label'], 'Label should default to null'); + } + protected function getExtensions(): array { $translationsType = $this->getConfiguredTranslationsType($this->locales, $this->defaultLocale, $this->requiredLocales);