Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2ceef59

Browse files
committed
feature #26825 [Form] Add choice_translation_locale option for Intl choice types (yceruto, fabpot)
This PR was merged into the 4.1-dev branch. Discussion ---------- [Form] Add choice_translation_locale option for Intl choice types | Q | A | ------------- | --- | Branch? | master (4.2) | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | yes | Tests pass? | yes | Fixed tickets | #26737 | License | MIT | Doc PR | symfony/symfony-docs#... This PR adds possibility to show the list of elements for a custom locale different to the current one and proposes also deprecate the `ChoiceLoaderInterface` implementation from all of them, moving it to a separate class. See related issue #26737 for full description and use-case. After: ```php $formBuilder->add('country', CountryType::class, [ 'choice_translation_locale' => 'es', ]); ``` Alternative of #23629 TODO: - [x] Update `UPGRADE-*.md` and `src/**/CHANGELOG.md` files. - [x] Add some tests. Commits ------- 9592fa6 moved feature to 4.1 e250dfa Add choice_translation_locale option for Intl choice types
2 parents 773c9df + 9592fa6 commit 2ceef59

File tree

12 files changed

+377
-4
lines changed

12 files changed

+377
-4
lines changed

UPGRADE-4.1.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,40 @@ EventDispatcher
2929

3030
* The `TraceableEventDispatcherInterface` has been deprecated.
3131

32+
Form
33+
----
34+
35+
* Deprecated the `ChoiceLoaderInterface` implementation in `CountryType`,
36+
`LanguageType`, `LocaleType` and `CurrencyType`, use the `choice_loader`
37+
option instead.
38+
39+
Before:
40+
```php
41+
class MyCountryType extends CountryType
42+
{
43+
public function loadChoiceList()
44+
{
45+
// override the method
46+
}
47+
}
48+
```
49+
50+
After:
51+
```php
52+
class MyCountryType extends AbstractType
53+
{
54+
public function getParent()
55+
{
56+
return CountryType::class;
57+
}
58+
59+
public function configureOptions(OptionsResolver $resolver)
60+
{
61+
$resolver->setDefault('choice_loader', ...); // override the option instead
62+
}
63+
}
64+
```
65+
3266
FrameworkBundle
3367
---------------
3468

src/Symfony/Component/Form/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ CHANGELOG
66

77
* added `input=datetime_immutable` to `DateType`, `TimeType`, `DateTimeType`
88
* added `rounding_mode` option to `MoneyType`
9+
* added `choice_translation_locale` option to `CountryType`, `LanguageType`, `LocaleType` and `CurrencyType`
10+
* deprecated the `ChoiceLoaderInterface` implementation in `CountryType`, `LanguageType`, `LocaleType` and `CurrencyType`
11+
* added `input=datetime_immutable` to DateType, TimeType, DateTimeType
12+
* added `rounding_mode` option to MoneyType
913

1014
4.0.0
1115
-----
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Form\ChoiceList\Loader;
13+
14+
/**
15+
* Callback choice loader optimized for Intl choice types.
16+
*
17+
* @author Jules Pietri <[email protected]>
18+
* @author Yonel Ceruto <[email protected]>
19+
*/
20+
class IntlCallbackChoiceLoader extends CallbackChoiceLoader
21+
{
22+
/**
23+
* {@inheritdoc}
24+
*/
25+
public function loadChoicesForValues(array $values, $value = null)
26+
{
27+
// Optimize
28+
$values = array_filter($values);
29+
if (empty($values)) {
30+
return array();
31+
}
32+
33+
// If no callable is set, values are the same as choices
34+
if (null === $value) {
35+
return $values;
36+
}
37+
38+
return $this->loadChoiceList($value)->getChoicesForValues($values);
39+
}
40+
41+
/**
42+
* {@inheritdoc}
43+
*/
44+
public function loadValuesForChoices(array $choices, $value = null)
45+
{
46+
// Optimize
47+
$choices = array_filter($choices);
48+
if (empty($choices)) {
49+
return array();
50+
}
51+
52+
// If no callable is set, choices are the same as values
53+
if (null === $value) {
54+
return $choices;
55+
}
56+
57+
return $this->loadChoiceList($value)->getValuesForChoices($choices);
58+
}
59+
}

src/Symfony/Component/Form/Extension/Core/Type/CountryType.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
use Symfony\Component\Form\AbstractType;
1515
use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
1616
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
17+
use Symfony\Component\Form\ChoiceList\Loader\IntlCallbackChoiceLoader;
1718
use Symfony\Component\Intl\Intl;
19+
use Symfony\Component\OptionsResolver\Options;
1820
use Symfony\Component\OptionsResolver\OptionsResolver;
1921

2022
class CountryType extends AbstractType implements ChoiceLoaderInterface
@@ -27,6 +29,8 @@ class CountryType extends AbstractType implements ChoiceLoaderInterface
2729
* {@link \Symfony\Component\Intl\Intl::getRegionBundle()}.
2830
*
2931
* @var ArrayChoiceList
32+
*
33+
* @deprecated since Symfony 4.1
3034
*/
3135
private $choiceList;
3236

@@ -36,9 +40,18 @@ class CountryType extends AbstractType implements ChoiceLoaderInterface
3640
public function configureOptions(OptionsResolver $resolver)
3741
{
3842
$resolver->setDefaults(array(
39-
'choice_loader' => $this,
43+
'choice_loader' => function (Options $options) {
44+
$choiceTranslationLocale = $options['choice_translation_locale'];
45+
46+
return new IntlCallbackChoiceLoader(function () use ($choiceTranslationLocale) {
47+
return array_flip(Intl::getRegionBundle()->getCountryNames($choiceTranslationLocale));
48+
});
49+
},
4050
'choice_translation_domain' => false,
51+
'choice_translation_locale' => null,
4152
));
53+
54+
$resolver->setAllowedTypes('choice_translation_locale', array('null', 'string'));
4255
}
4356

4457
/**
@@ -59,9 +72,13 @@ public function getBlockPrefix()
5972

6073
/**
6174
* {@inheritdoc}
75+
*
76+
* @deprecated since Symfony 4.1
6277
*/
6378
public function loadChoiceList($value = null)
6479
{
80+
@trigger_error(sprintf('Method "%s" is deprecated since Symfony 4.1, use "choice_loader" option instead.', __METHOD__), E_USER_DEPRECATED);
81+
6582
if (null !== $this->choiceList) {
6683
return $this->choiceList;
6784
}
@@ -71,9 +88,13 @@ public function loadChoiceList($value = null)
7188

7289
/**
7390
* {@inheritdoc}
91+
*
92+
* @deprecated since Symfony 4.1
7493
*/
7594
public function loadChoicesForValues(array $values, $value = null)
7695
{
96+
@trigger_error(sprintf('Method "%s" is deprecated since Symfony 4.1, use "choice_loader" option instead.', __METHOD__), E_USER_DEPRECATED);
97+
7798
// Optimize
7899
$values = array_filter($values);
79100
if (empty($values)) {
@@ -90,9 +111,13 @@ public function loadChoicesForValues(array $values, $value = null)
90111

91112
/**
92113
* {@inheritdoc}
114+
*
115+
* @deprecated since Symfony 4.1
93116
*/
94117
public function loadValuesForChoices(array $choices, $value = null)
95118
{
119+
@trigger_error(sprintf('Method "%s" is deprecated since Symfony 4.1, use "choice_loader" option instead.', __METHOD__), E_USER_DEPRECATED);
120+
96121
// Optimize
97122
$choices = array_filter($choices);
98123
if (empty($choices)) {

src/Symfony/Component/Form/Extension/Core/Type/CurrencyType.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
use Symfony\Component\Form\AbstractType;
1515
use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
1616
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
17+
use Symfony\Component\Form\ChoiceList\Loader\IntlCallbackChoiceLoader;
1718
use Symfony\Component\Intl\Intl;
19+
use Symfony\Component\OptionsResolver\Options;
1820
use Symfony\Component\OptionsResolver\OptionsResolver;
1921

2022
class CurrencyType extends AbstractType implements ChoiceLoaderInterface
@@ -27,6 +29,8 @@ class CurrencyType extends AbstractType implements ChoiceLoaderInterface
2729
* {@link \Symfony\Component\Intl\Intl::getCurrencyBundle()}.
2830
*
2931
* @var ArrayChoiceList
32+
*
33+
* @deprecated since Symfony 4.1
3034
*/
3135
private $choiceList;
3236

@@ -36,9 +40,18 @@ class CurrencyType extends AbstractType implements ChoiceLoaderInterface
3640
public function configureOptions(OptionsResolver $resolver)
3741
{
3842
$resolver->setDefaults(array(
39-
'choice_loader' => $this,
43+
'choice_loader' => function (Options $options) {
44+
$choiceTranslationLocale = $options['choice_translation_locale'];
45+
46+
return new IntlCallbackChoiceLoader(function () use ($choiceTranslationLocale) {
47+
return array_flip(Intl::getCurrencyBundle()->getCurrencyNames($choiceTranslationLocale));
48+
});
49+
},
4050
'choice_translation_domain' => false,
51+
'choice_translation_locale' => null,
4152
));
53+
54+
$resolver->setAllowedTypes('choice_translation_locale', array('null', 'string'));
4255
}
4356

4457
/**
@@ -59,9 +72,13 @@ public function getBlockPrefix()
5972

6073
/**
6174
* {@inheritdoc}
75+
*
76+
* @deprecated since Symfony 4.1
6277
*/
6378
public function loadChoiceList($value = null)
6479
{
80+
@trigger_error(sprintf('Method "%s" is deprecated since Symfony 4.1, use "choice_loader" option instead.', __METHOD__), E_USER_DEPRECATED);
81+
6582
if (null !== $this->choiceList) {
6683
return $this->choiceList;
6784
}
@@ -71,9 +88,13 @@ public function loadChoiceList($value = null)
7188

7289
/**
7390
* {@inheritdoc}
91+
*
92+
* @deprecated since Symfony 4.1
7493
*/
7594
public function loadChoicesForValues(array $values, $value = null)
7695
{
96+
@trigger_error(sprintf('Method "%s" is deprecated since Symfony 4.1, use "choice_loader" option instead.', __METHOD__), E_USER_DEPRECATED);
97+
7798
// Optimize
7899
$values = array_filter($values);
79100
if (empty($values)) {
@@ -90,9 +111,13 @@ public function loadChoicesForValues(array $values, $value = null)
90111

91112
/**
92113
* {@inheritdoc}
114+
*
115+
* @deprecated since Symfony 4.1
93116
*/
94117
public function loadValuesForChoices(array $choices, $value = null)
95118
{
119+
@trigger_error(sprintf('Method "%s" is deprecated since Symfony 4.1, use "choice_loader" option instead.', __METHOD__), E_USER_DEPRECATED);
120+
96121
// Optimize
97122
$choices = array_filter($choices);
98123
if (empty($choices)) {

src/Symfony/Component/Form/Extension/Core/Type/LanguageType.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
use Symfony\Component\Form\AbstractType;
1515
use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
1616
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
17+
use Symfony\Component\Form\ChoiceList\Loader\IntlCallbackChoiceLoader;
1718
use Symfony\Component\Intl\Intl;
19+
use Symfony\Component\OptionsResolver\Options;
1820
use Symfony\Component\OptionsResolver\OptionsResolver;
1921

2022
class LanguageType extends AbstractType implements ChoiceLoaderInterface
@@ -27,6 +29,8 @@ class LanguageType extends AbstractType implements ChoiceLoaderInterface
2729
* {@link \Symfony\Component\Intl\Intl::getLanguageBundle()}.
2830
*
2931
* @var ArrayChoiceList
32+
*
33+
* @deprecated since Symfony 4.1
3034
*/
3135
private $choiceList;
3236

@@ -36,9 +40,18 @@ class LanguageType extends AbstractType implements ChoiceLoaderInterface
3640
public function configureOptions(OptionsResolver $resolver)
3741
{
3842
$resolver->setDefaults(array(
39-
'choice_loader' => $this,
43+
'choice_loader' => function (Options $options) {
44+
$choiceTranslationLocale = $options['choice_translation_locale'];
45+
46+
return new IntlCallbackChoiceLoader(function () use ($choiceTranslationLocale) {
47+
return array_flip(Intl::getLanguageBundle()->getLanguageNames($choiceTranslationLocale));
48+
});
49+
},
4050
'choice_translation_domain' => false,
51+
'choice_translation_locale' => null,
4152
));
53+
54+
$resolver->setAllowedTypes('choice_translation_locale', array('null', 'string'));
4255
}
4356

4457
/**
@@ -59,9 +72,13 @@ public function getBlockPrefix()
5972

6073
/**
6174
* {@inheritdoc}
75+
*
76+
* @deprecated since Symfony 4.1
6277
*/
6378
public function loadChoiceList($value = null)
6479
{
80+
@trigger_error(sprintf('Method "%s" is deprecated since Symfony 4.1, use "choice_loader" option instead.', __METHOD__), E_USER_DEPRECATED);
81+
6582
if (null !== $this->choiceList) {
6683
return $this->choiceList;
6784
}
@@ -71,9 +88,13 @@ public function loadChoiceList($value = null)
7188

7289
/**
7390
* {@inheritdoc}
91+
*
92+
* @deprecated since Symfony 4.1
7493
*/
7594
public function loadChoicesForValues(array $values, $value = null)
7695
{
96+
@trigger_error(sprintf('Method "%s" is deprecated since Symfony 4.1, use "choice_loader" option instead.', __METHOD__), E_USER_DEPRECATED);
97+
7798
// Optimize
7899
$values = array_filter($values);
79100
if (empty($values)) {
@@ -90,9 +111,13 @@ public function loadChoicesForValues(array $values, $value = null)
90111

91112
/**
92113
* {@inheritdoc}
114+
*
115+
* @deprecated since Symfony 4.1
93116
*/
94117
public function loadValuesForChoices(array $choices, $value = null)
95118
{
119+
@trigger_error(sprintf('Method "%s" is deprecated since Symfony 4.1, use "choice_loader" option instead.', __METHOD__), E_USER_DEPRECATED);
120+
96121
// Optimize
97122
$choices = array_filter($choices);
98123
if (empty($choices)) {

0 commit comments

Comments
 (0)