Skip to content

Commit

Permalink
Add translatable locale labels (Should fix #262) (#365)
Browse files Browse the repository at this point in the history
  • Loading branch information
tchapi authored Dec 21, 2021
1 parent 7e17244 commit 88ff44b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Form/EventListener/TranslationsListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
1 change: 1 addition & 0 deletions src/Form/Type/TranslationsType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
20 changes: 20 additions & 0 deletions tests/Form/Type/TranslationsTypeAdvancedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 88ff44b

Please sign in to comment.