Skip to content

[Form][LanguageType] Add whitelist option #35456

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Component/Form/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ CHANGELOG

* The `view_timezone` option defaults to the `model_timezone` if no `reference_date` is configured.
* Added default `inputmode` attribute to Search, Email and Tel form types.
* Added the `whitelist` option to `LanguageType` to filter the available choices.

5.0.0
-----
Expand Down
29 changes: 18 additions & 11 deletions src/Symfony/Component/Form/Extension/Core/Type/LanguageType.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,25 @@ public function configureOptions(OptionsResolver $resolver)
'choice_loader' => function (Options $options) {
$choiceTranslationLocale = $options['choice_translation_locale'];
$useAlpha3Codes = $options['alpha3'];
$choiceSelfTranslation = $options['choice_self_translation'];
$useChoiceSelfTranslation = true === $options['choice_self_translation'];
$whitelist = $options['whitelist'];

return new IntlCallbackChoiceLoader(function () use ($choiceTranslationLocale, $useAlpha3Codes, $choiceSelfTranslation) {
if (true === $choiceSelfTranslation) {
foreach (Languages::getLanguageCodes() as $alpha2Code) {
try {
$languageCode = $useAlpha3Codes ? Languages::getAlpha3Code($alpha2Code) : $alpha2Code;
$languagesList[$languageCode] = Languages::getName($alpha2Code, $alpha2Code);
} catch (MissingResourceException $e) {
// ignore errors like "Couldn't read the indices for the locale 'meta'"
return new IntlCallbackChoiceLoader(function () use ($choiceTranslationLocale, $useAlpha3Codes, $useChoiceSelfTranslation, $whitelist) {
if ($whitelist) {
$whitelist = array_flip($whitelist);
}

foreach (Languages::getLanguageCodes() as $alpha2Code) {
try {
$languageCode = $useAlpha3Codes ? Languages::getAlpha3Code($alpha2Code) : $alpha2Code;
if ($whitelist && !isset($whitelist[$languageCode])) {
continue;
}

$languagesList[$languageCode] = Languages::getName($alpha2Code, $useChoiceSelfTranslation ? $alpha2Code : $choiceTranslationLocale);
} catch (MissingResourceException $e) {
// ignore errors like "Couldn't read the indices for the locale 'meta'"
}
} else {
$languagesList = $useAlpha3Codes ? Languages::getAlpha3Names($choiceTranslationLocale) : Languages::getNames($choiceTranslationLocale);
}

return array_flip($languagesList);
Expand All @@ -53,11 +58,13 @@ public function configureOptions(OptionsResolver $resolver)
'choice_translation_locale' => null,
'alpha3' => false,
'choice_self_translation' => false,
'whitelist' => null,
]);

$resolver->setAllowedTypes('choice_self_translation', ['bool']);
$resolver->setAllowedTypes('choice_translation_locale', ['null', 'string']);
$resolver->setAllowedTypes('alpha3', 'bool');
$resolver->setAllowedTypes('whitelist', ['null', 'array']);

$resolver->setNormalizer('choice_self_translation', function (Options $options, $value) {
if (true === $value && $options['choice_translation_locale']) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,21 @@ public function testSubmitNullUsesDefaultEmptyData($emptyData = 'en', $expectedD
{
parent::testSubmitNullUsesDefaultEmptyData($emptyData, $expectedData);
}

public function testWhitelist()
{
$choices = $this->factory
->create(static::TESTED_TYPE, null, [
'whitelist' => [
'fr',
'de',
],
])
->createView()->vars['choices'];

$this->assertContainsEquals(new ChoiceView('fr', 'fr', 'French'), $choices);
$this->assertContainsEquals(new ChoiceView('de', 'de', 'German'), $choices);
$this->assertNotContainsEquals(new ChoiceView('en', 'en', 'English'), $choices);
$this->assertNotContainsEquals(new ChoiceView('es', 'es', 'Spanish'), $choices);
}
}