Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 9b64be8

Browse filesBrowse files
committed
feature #38469 [Form] Add "choice_translation_parameters" option (VincentLanglet)
This PR was squashed before being merged into the 5.3-dev branch. Discussion ---------- [Form] Add "choice_translation_parameters" option | Q | A | ------------- | --- | Branch? | 5.x | Bug fix? | no | New feature? | yes <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | Fix #36845 <!-- prefix each issue number with "Fix #", if any --> | License | MIT | Doc PR | symfony/symfony-docs#13677 Original PR: #36851 Commits ------- 1ce5b03 [Form] Add "choice_translation_parameters" option
2 parents b82bc85 + 1ce5b03 commit 9b64be8
Copy full SHA for 9b64be8

15 files changed

+322
-81
lines changed

‎src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585
{{- block('choice_widget_options') -}}
8686
</optgroup>
8787
{%- else -%}
88-
<option value="{{ choice.value }}"{% if choice.attr %}{% with { attr: choice.attr } %}{{ block('attributes') }}{% endwith %}{% endif %}{% if not render_preferred_choices|default(false) and choice is selectedchoice(value) %} selected="selected"{% endif %}>{{ choice_translation_domain is same as(false) ? choice.label : choice.label|trans({}, choice_translation_domain) }}</option>
88+
<option value="{{ choice.value }}"{% if choice.attr %}{% with { attr: choice.attr } %}{{ block('attributes') }}{% endwith %}{% endif %}{% if not render_preferred_choices|default(false) and choice is selectedchoice(value) %} selected="selected"{% endif %}>{{ choice_translation_domain is same as(false) ? choice.label : choice.label|trans(choice.labelTranslationParameters, choice_translation_domain) }}</option>
8989
{%- endif -%}
9090
{% endfor %}
9191
{%- endblock choice_widget_options -%}

‎src/Symfony/Bridge/Twig/composer.json

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Twig/composer.json
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"symfony/asset": "^4.4|^5.0",
2828
"symfony/dependency-injection": "^4.4|^5.0",
2929
"symfony/finder": "^4.4|^5.0",
30-
"symfony/form": "^5.1.9",
30+
"symfony/form": "^5.3",
3131
"symfony/http-foundation": "^4.4|^5.0",
3232
"symfony/http-kernel": "^4.4|^5.0",
3333
"symfony/mime": "^5.2",
@@ -52,7 +52,7 @@
5252
},
5353
"conflict": {
5454
"symfony/console": "<4.4",
55-
"symfony/form": "<5.1",
55+
"symfony/form": "<5.3",
5656
"symfony/http-foundation": "<4.4",
5757
"symfony/http-kernel": "<4.4",
5858
"symfony/translation": "<5.2",

‎src/Symfony/Component/Form/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ CHANGELOG
1212
* Deprecated passing an array as the first argument of the `CheckboxListMapper::mapFormsToData()` method, pass `\Traversable` instead.
1313
* Deprecated passing an array as the second argument of the `RadioListMapper::mapDataToForms()` method, pass `\Traversable` instead.
1414
* Deprecated passing an array as the first argument of the `RadioListMapper::mapFormsToData()` method, pass `\Traversable` instead.
15+
* Added a `choice_translation_parameters` option to `ChoiceType`
1516

1617
5.2.0
1718
-----

‎src/Symfony/Component/Form/ChoiceList/ChoiceList.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/ChoiceList/ChoiceList.php
+13Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Form\ChoiceList\Factory\Cache\ChoiceFilter;
1717
use Symfony\Component\Form\ChoiceList\Factory\Cache\ChoiceLabel;
1818
use Symfony\Component\Form\ChoiceList\Factory\Cache\ChoiceLoader;
19+
use Symfony\Component\Form\ChoiceList\Factory\Cache\ChoiceTranslationParameters;
1920
use Symfony\Component\Form\ChoiceList\Factory\Cache\ChoiceValue;
2021
use Symfony\Component\Form\ChoiceList\Factory\Cache\GroupBy;
2122
use Symfony\Component\Form\ChoiceList\Factory\Cache\PreferredChoice;
@@ -113,6 +114,18 @@ public static function attr($formType, $attr, $vary = null): ChoiceAttr
113114
return new ChoiceAttr($formType, $attr, $vary);
114115
}
115116

117+
/**
118+
* Decorates a "choice_translation_parameters" option to make it cacheable.
119+
*
120+
* @param FormTypeInterface|FormTypeExtensionInterface $formType A form type or type extension configuring a cacheable choice list
121+
* @param callable|array $translationParameters Any pseudo callable or array to create translation parameters from a choice
122+
* @param mixed|null $vary Dynamic data used to compute a unique hash when caching the option
123+
*/
124+
public static function translationParameters($formType, $translationParameters, $vary = null): ChoiceTranslationParameters
125+
{
126+
return new ChoiceTranslationParameters($formType, $translationParameters, $vary);
127+
}
128+
116129
/**
117130
* Decorates a "group_by" callback to make it cacheable.
118131
*
+27Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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\Factory\Cache;
13+
14+
use Symfony\Component\Form\FormTypeExtensionInterface;
15+
use Symfony\Component\Form\FormTypeInterface;
16+
17+
/**
18+
* A cacheable wrapper for any {@see FormTypeInterface} or {@see FormTypeExtensionInterface}
19+
* which configures a "choice_translation_parameters" option.
20+
*
21+
* @internal
22+
*
23+
* @author Vincent Langlet <vincentlanglet@users.noreply.github.com>
24+
*/
25+
final class ChoiceTranslationParameters extends AbstractStaticOption
26+
{
27+
}

‎src/Symfony/Component/Form/ChoiceList/Factory/CachingFactoryDecorator.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/ChoiceList/Factory/CachingFactoryDecorator.php
+26-9Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -174,14 +174,16 @@ public function createListFromLoader(ChoiceLoaderInterface $loader, $value = nul
174174
/**
175175
* {@inheritdoc}
176176
*
177-
* @param array|callable|Cache\PreferredChoice|null $preferredChoices The preferred choices
178-
* @param callable|false|Cache\ChoiceLabel|null $label The option or static option generating the choice labels
179-
* @param callable|Cache\ChoiceFieldName|null $index The option or static option generating the view indices
180-
* @param callable|Cache\GroupBy|null $groupBy The option or static option generating the group names
181-
* @param array|callable|Cache\ChoiceAttr|null $attr The option or static option generating the HTML attributes
177+
* @param array|callable|Cache\PreferredChoice|null $preferredChoices The preferred choices
178+
* @param callable|false|Cache\ChoiceLabel|null $label The option or static option generating the choice labels
179+
* @param callable|Cache\ChoiceFieldName|null $index The option or static option generating the view indices
180+
* @param callable|Cache\GroupBy|null $groupBy The option or static option generating the group names
181+
* @param array|callable|Cache\ChoiceAttr|null $attr The option or static option generating the HTML attributes
182+
* @param array|callable|Cache\ChoiceTranslationParameters $labelTranslationParameters The parameters used to translate the choice labels
182183
*/
183-
public function createView(ChoiceListInterface $list, $preferredChoices = null, $label = null, $index = null, $groupBy = null, $attr = null)
184+
public function createView(ChoiceListInterface $list, $preferredChoices = null, $label = null, $index = null, $groupBy = null, $attr = null/*, $labelTranslationParameters = []*/)
184185
{
186+
$labelTranslationParameters = \func_num_args() > 6 ? func_get_arg(6) : [];
185187
$cache = true;
186188

187189
if ($preferredChoices instanceof Cache\PreferredChoice) {
@@ -214,11 +216,25 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,
214216
$cache = false;
215217
}
216218

219+
if ($labelTranslationParameters instanceof Cache\ChoiceTranslationParameters) {
220+
$labelTranslationParameters = $labelTranslationParameters->getOption();
221+
} elseif ([] !== $labelTranslationParameters) {
222+
$cache = false;
223+
}
224+
217225
if (!$cache) {
218-
return $this->decoratedFactory->createView($list, $preferredChoices, $label, $index, $groupBy, $attr);
226+
return $this->decoratedFactory->createView(
227+
$list,
228+
$preferredChoices,
229+
$label,
230+
$index,
231+
$groupBy,
232+
$attr,
233+
$labelTranslationParameters
234+
);
219235
}
220236

221-
$hash = self::generateHash([$list, $preferredChoices, $label, $index, $groupBy, $attr]);
237+
$hash = self::generateHash([$list, $preferredChoices, $label, $index, $groupBy, $attr, $labelTranslationParameters]);
222238

223239
if (!isset($this->views[$hash])) {
224240
$this->views[$hash] = $this->decoratedFactory->createView(
@@ -227,7 +243,8 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,
227243
$label,
228244
$index,
229245
$groupBy,
230-
$attr
246+
$attr,
247+
$labelTranslationParameters
231248
);
232249
}
233250

‎src/Symfony/Component/Form/ChoiceList/Factory/ChoiceListFactoryInterface.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/ChoiceList/Factory/ChoiceListFactoryInterface.php
+6-5Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,13 @@ public function createListFromLoader(ChoiceLoaderInterface $loader, callable $va
7676
* match the keys of the choices. The values should be arrays of HTML
7777
* attributes that should be added to the respective choice.
7878
*
79-
* @param array|callable|null $preferredChoices The preferred choices
80-
* @param callable|false|null $label The callable generating the choice labels;
81-
* pass false to discard the label
82-
* @param array|callable|null $attr The callable generating the HTML attributes
79+
* @param array|callable|null $preferredChoices The preferred choices
80+
* @param callable|false|null $label The callable generating the choice labels;
81+
* pass false to discard the label
82+
* @param array|callable|null $attr The callable generating the HTML attributes
83+
* @param array|callable $labelTranslationParameters The parameters used to translate the choice labels
8384
*
8485
* @return ChoiceListView The choice list view
8586
*/
86-
public function createView(ChoiceListInterface $list, $preferredChoices = null, $label = null, callable $index = null, callable $groupBy = null, $attr = null);
87+
public function createView(ChoiceListInterface $list, $preferredChoices = null, $label = null, callable $index = null, callable $groupBy = null, $attr = null/*, $labelTranslationParameters = []*/);
8788
}

‎src/Symfony/Component/Form/ChoiceList/Factory/DefaultChoiceListFactory.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/ChoiceList/Factory/DefaultChoiceListFactory.php
+17-5Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,12 @@ public function createListFromLoader(ChoiceLoaderInterface $loader, callable $va
6868

6969
/**
7070
* {@inheritdoc}
71+
*
72+
* @param array|callable $labelTranslationParameters The parameters used to translate the choice labels
7173
*/
72-
public function createView(ChoiceListInterface $list, $preferredChoices = null, $label = null, callable $index = null, callable $groupBy = null, $attr = null)
74+
public function createView(ChoiceListInterface $list, $preferredChoices = null, $label = null, callable $index = null, callable $groupBy = null, $attr = null/*, $labelTranslationParameters = []*/)
7375
{
76+
$labelTranslationParameters = \func_num_args() > 6 ? func_get_arg(6) : [];
7477
$preferredViews = [];
7578
$preferredViewsOrder = [];
7679
$otherViews = [];
@@ -109,6 +112,7 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,
109112
$keys,
110113
$index,
111114
$attr,
115+
$labelTranslationParameters,
112116
$preferredChoices,
113117
$preferredViews,
114118
$preferredViewsOrder,
@@ -146,6 +150,7 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,
146150
$keys,
147151
$index,
148152
$attr,
153+
$labelTranslationParameters,
149154
$preferredChoices,
150155
$preferredViews,
151156
$preferredViewsOrder,
@@ -162,7 +167,7 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,
162167
return new ChoiceListView($otherViews, $preferredViews);
163168
}
164169

165-
private static function addChoiceView($choice, string $value, $label, array $keys, &$index, $attr, ?callable $isPreferred, array &$preferredViews, array &$preferredViewsOrder, array &$otherViews)
170+
private static function addChoiceView($choice, string $value, $label, array $keys, &$index, $attr, $labelTranslationParameters, ?callable $isPreferred, array &$preferredViews, array &$preferredViewsOrder, array &$otherViews)
166171
{
167172
// $value may be an integer or a string, since it's stored in the array
168173
// keys. We want to guarantee it's a string though.
@@ -186,7 +191,10 @@ private static function addChoiceView($choice, string $value, $label, array $key
186191
$label,
187192
// The attributes may be a callable or a mapping from choice indices
188193
// to nested arrays
189-
\is_callable($attr) ? $attr($choice, $key, $value) : (isset($attr[$key]) ? $attr[$key] : [])
194+
\is_callable($attr) ? $attr($choice, $key, $value) : ($attr[$key] ?? []),
195+
// The label translation parameters may be a callable or a mapping from choice indices
196+
// to nested arrays
197+
\is_callable($labelTranslationParameters) ? $labelTranslationParameters($choice, $key, $value) : ($labelTranslationParameters[$key] ?? [])
190198
);
191199

192200
// $isPreferred may be null if no choices are preferred
@@ -198,7 +206,7 @@ private static function addChoiceView($choice, string $value, $label, array $key
198206
$otherViews[$nextIndex] = $view;
199207
}
200208

201-
private static function addChoiceViewsFromStructuredValues(array $values, $label, array $choices, array $keys, &$index, $attr, ?callable $isPreferred, array &$preferredViews, array &$preferredViewsOrder, array &$otherViews)
209+
private static function addChoiceViewsFromStructuredValues(array $values, $label, array $choices, array $keys, &$index, $attr, $labelTranslationParameters, ?callable $isPreferred, array &$preferredViews, array &$preferredViewsOrder, array &$otherViews)
202210
{
203211
foreach ($values as $key => $value) {
204212
if (null === $value) {
@@ -217,6 +225,7 @@ private static function addChoiceViewsFromStructuredValues(array $values, $label
217225
$keys,
218226
$index,
219227
$attr,
228+
$labelTranslationParameters,
220229
$isPreferred,
221230
$preferredViewsForGroup,
222231
$preferredViewsOrder,
@@ -242,6 +251,7 @@ private static function addChoiceViewsFromStructuredValues(array $values, $label
242251
$keys,
243252
$index,
244253
$attr,
254+
$labelTranslationParameters,
245255
$isPreferred,
246256
$preferredViews,
247257
$preferredViewsOrder,
@@ -250,7 +260,7 @@ private static function addChoiceViewsFromStructuredValues(array $values, $label
250260
}
251261
}
252262

253-
private static function addChoiceViewsGroupedByCallable(callable $groupBy, $choice, string $value, $label, array $keys, &$index, $attr, ?callable $isPreferred, array &$preferredViews, array &$preferredViewsOrder, array &$otherViews)
263+
private static function addChoiceViewsGroupedByCallable(callable $groupBy, $choice, string $value, $label, array $keys, &$index, $attr, $labelTranslationParameters, ?callable $isPreferred, array &$preferredViews, array &$preferredViewsOrder, array &$otherViews)
254264
{
255265
$groupLabels = $groupBy($choice, $keys[$value], $value);
256266

@@ -263,6 +273,7 @@ private static function addChoiceViewsGroupedByCallable(callable $groupBy, $choi
263273
$keys,
264274
$index,
265275
$attr,
276+
$labelTranslationParameters,
266277
$isPreferred,
267278
$preferredViews,
268279
$preferredViewsOrder,
@@ -292,6 +303,7 @@ private static function addChoiceViewsGroupedByCallable(callable $groupBy, $choi
292303
$keys,
293304
$index,
294305
$attr,
306+
$labelTranslationParameters,
295307
$isPreferred,
296308
$preferredViews[$groupLabel]->choices,
297309
$preferredViewsOrder[$groupLabel],

‎src/Symfony/Component/Form/ChoiceList/Factory/PropertyAccessDecorator.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/ChoiceList/Factory/PropertyAccessDecorator.php
+27-7Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -145,16 +145,18 @@ public function createListFromLoader(ChoiceLoaderInterface $loader, $value = nul
145145
/**
146146
* {@inheritdoc}
147147
*
148-
* @param array|callable|string|PropertyPath|null $preferredChoices The preferred choices
149-
* @param callable|string|PropertyPath|null $label The callable or path generating the choice labels
150-
* @param callable|string|PropertyPath|null $index The callable or path generating the view indices
151-
* @param callable|string|PropertyPath|null $groupBy The callable or path generating the group names
152-
* @param array|callable|string|PropertyPath|null $attr The callable or path generating the HTML attributes
148+
* @param array|callable|string|PropertyPath|null $preferredChoices The preferred choices
149+
* @param callable|string|PropertyPath|null $label The callable or path generating the choice labels
150+
* @param callable|string|PropertyPath|null $index The callable or path generating the view indices
151+
* @param callable|string|PropertyPath|null $groupBy The callable or path generating the group names
152+
* @param array|callable|string|PropertyPath|null $attr The callable or path generating the HTML attributes
153+
* @param array|callable|string|PropertyPath $labelTranslationParameters The callable or path generating the parameters used to translate the choice labels
153154
*
154155
* @return ChoiceListView The choice list view
155156
*/
156-
public function createView(ChoiceListInterface $list, $preferredChoices = null, $label = null, $index = null, $groupBy = null, $attr = null)
157+
public function createView(ChoiceListInterface $list, $preferredChoices = null, $label = null, $index = null, $groupBy = null, $attr = null/*, $labelTranslationParameters = []*/)
157158
{
159+
$labelTranslationParameters = \func_num_args() > 6 ? func_get_arg(6) : [];
158160
$accessor = $this->propertyAccessor;
159161

160162
if (\is_string($label)) {
@@ -217,6 +219,24 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,
217219
};
218220
}
219221

220-
return $this->decoratedFactory->createView($list, $preferredChoices, $label, $index, $groupBy, $attr);
222+
if (\is_string($labelTranslationParameters)) {
223+
$labelTranslationParameters = new PropertyPath($labelTranslationParameters);
224+
}
225+
226+
if ($labelTranslationParameters instanceof PropertyPath) {
227+
$labelTranslationParameters = static function ($choice) use ($accessor, $labelTranslationParameters) {
228+
return $accessor->getValue($choice, $labelTranslationParameters);
229+
};
230+
}
231+
232+
return $this->decoratedFactory->createView(
233+
$list,
234+
$preferredChoices,
235+
$label,
236+
$index,
237+
$groupBy,
238+
$attr,
239+
$labelTranslationParameters
240+
);
221241
}
222242
}

‎src/Symfony/Component/Form/ChoiceList/View/ChoiceView.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/ChoiceList/View/ChoiceView.php
+12-5Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,26 @@ class ChoiceView
2727
*/
2828
public $attr;
2929

30+
/**
31+
* Additional parameters used to translate the label.
32+
*/
33+
public $labelTranslationParameters;
34+
3035
/**
3136
* Creates a new choice view.
3237
*
33-
* @param mixed $data The original choice
34-
* @param string $value The view representation of the choice
35-
* @param string|false $label The label displayed to humans; pass false to discard the label
36-
* @param array $attr Additional attributes for the HTML tag
38+
* @param mixed $data The original choice
39+
* @param string $value The view representation of the choice
40+
* @param string|false $label The label displayed to humans; pass false to discard the label
41+
* @param array $attr Additional attributes for the HTML tag
42+
* @param array $labelTranslationParameters Additional parameters used to translate the label
3743
*/
38-
public function __construct($data, string $value, $label, array $attr = [])
44+
public function __construct($data, string $value, $label, array $attr = [], array $labelTranslationParameters = [])
3945
{
4046
$this->data = $data;
4147
$this->value = $value;
4248
$this->label = $label;
4349
$this->attr = $attr;
50+
$this->labelTranslationParameters = $labelTranslationParameters;
4451
}
4552
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.