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 f31e0c4

Browse filesBrowse files
Make choice_translation_parameters similar to choice_attr and support callable
1 parent 49c02b4 commit f31e0c4
Copy full SHA for f31e0c4

15 files changed

+316
-91
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Twig/Resources/views/Form/bootstrap_4_layout.html.twig
-2Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,8 @@
199199
{% block choice_widget_expanded -%}
200200
<div {{ block('widget_container_attributes') }}>
201201
{%- for child in form %}
202-
{# NEXT_MAJOR: Use default([]) for choice_translation_parameters #}
203202
{{- form_widget(child, {
204203
parent_label_class: label_attr.class|default(''),
205-
label_translation_parameters: choice_translation_parameters|default(label_translation_parameters),
206204
translation_domain: choice_translation_domain,
207205
valid: valid,
208206
}) -}}

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

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Twig/Resources/views/Form/bootstrap_base_layout.html.twig
-4Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,20 +151,16 @@
151151
{% block choice_widget_expanded -%}
152152
{%- if '-inline' in label_attr.class|default('') -%}
153153
{%- for child in form %}
154-
{# NEXT_MAJOR: Use default([]) for choice_translation_parameters #}
155154
{{- form_widget(child, {
156155
parent_label_class: label_attr.class|default(''),
157-
label_translation_parameters: choice_translation_parameters|default(label_translation_parameters),
158156
translation_domain: choice_translation_domain,
159157
}) -}}
160158
{% endfor -%}
161159
{%- else -%}
162160
<div {{ block('widget_container_attributes') }}>
163161
{%- for child in form %}
164-
{# NEXT_MAJOR: Use default([]) for choice_translation_parameters #}
165162
{{- form_widget(child, {
166163
parent_label_class: label_attr.class|default(''),
167-
label_translation_parameters: choice_translation_parameters|default(label_translation_parameters),
168164
translation_domain: choice_translation_domain,
169165
}) -}}
170166
{%- endfor -%}

‎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
+3-6Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@
5050
<div {{ block('widget_container_attributes') }}>
5151
{%- for child in form %}
5252
{{- form_widget(child) -}}
53-
{# NEXT_MAJOR: Use default([]) for choice_translation_parameters #}
54-
{{- form_label(child, null, {label_translation_parameters: choice_translation_parameters|default(label_translation_parameters), translation_domain: choice_translation_domain}) -}}
53+
{{- form_label(child, null, {translation_domain: choice_translation_domain}) -}}
5554
{% endfor -%}
5655
</div>
5756
{%- endblock choice_widget_expanded -%}
@@ -81,14 +80,12 @@
8180
{%- block choice_widget_options -%}
8281
{% for group_label, choice in options %}
8382
{%- if choice is iterable -%}
84-
{# NEXT_MAJOR: Use default([]) for choice_translation_parameters #}
85-
<optgroup label="{{ choice_translation_domain is same as(false) ? group_label : group_label|trans(choice_translation_parameters|default(label_translation_parameters), choice_translation_domain) }}">
83+
<optgroup label="{{ choice_translation_domain is same as(false) ? group_label : group_label|trans({}, choice_translation_domain) }}">
8684
{% set options = choice %}
8785
{{- block('choice_widget_options') -}}
8886
</optgroup>
8987
{%- else -%}
90-
{# NEXT_MAJOR: Use default([]) for choice_translation_parameters #}
91-
<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_parameters|default(label_translation_parameters), 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>
9289
{%- endif -%}
9390
{% endfor %}
9491
{%- endblock choice_widget_options -%}

‎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
+25-9Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -174,13 +174,14 @@ 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
{
185186
$cache = true;
186187

@@ -214,11 +215,25 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,
214215
$cache = false;
215216
}
216217

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

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

223238
if (!isset($this->views[$hash])) {
224239
$this->views[$hash] = $this->decoratedFactory->createView(
@@ -227,7 +242,8 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,
227242
$label,
228243
$index,
229244
$groupBy,
230-
$attr
245+
$attr,
246+
$labelTranslationParameters
231247
);
232248
}
233249

‎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
+16-5Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function createListFromLoader(ChoiceLoaderInterface $loader, callable $va
6969
/**
7070
* {@inheritdoc}
7171
*/
72-
public function createView(ChoiceListInterface $list, $preferredChoices = null, $label = null, callable $index = null, callable $groupBy = null, $attr = null)
72+
public function createView(ChoiceListInterface $list, $preferredChoices = null, $label = null, callable $index = null, callable $groupBy = null, $attr = null, $labelTranslationParameters = [])
7373
{
7474
$preferredViews = [];
7575
$preferredViewsOrder = [];
@@ -109,6 +109,7 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,
109109
$keys,
110110
$index,
111111
$attr,
112+
$labelTranslationParameters,
112113
$preferredChoices,
113114
$preferredViews,
114115
$preferredViewsOrder,
@@ -146,6 +147,7 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,
146147
$keys,
147148
$index,
148149
$attr,
150+
$labelTranslationParameters,
149151
$preferredChoices,
150152
$preferredViews,
151153
$preferredViewsOrder,
@@ -162,7 +164,7 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,
162164
return new ChoiceListView($otherViews, $preferredViews);
163165
}
164166

165-
private static function addChoiceView($choice, string $value, $label, array $keys, &$index, $attr, ?callable $isPreferred, array &$preferredViews, array &$preferredViewsOrder, array &$otherViews)
167+
private static function addChoiceView($choice, string $value, $label, array $keys, &$index, $attr, $labelTranslationParameters, ?callable $isPreferred, array &$preferredViews, array &$preferredViewsOrder, array &$otherViews)
166168
{
167169
// $value may be an integer or a string, since it's stored in the array
168170
// keys. We want to guarantee it's a string though.
@@ -186,7 +188,12 @@ private static function addChoiceView($choice, string $value, $label, array $key
186188
$label,
187189
// The attributes may be a callable or a mapping from choice indices
188190
// to nested arrays
189-
\is_callable($attr) ? $attr($choice, $key, $value) : (isset($attr[$key]) ? $attr[$key] : [])
191+
\is_callable($attr) ? $attr($choice, $key, $value) : (isset($attr[$key]) ? $attr[$key] : []),
192+
// The label translation parameters may be a callable or a mapping from choice indices
193+
// to nested arrays
194+
\is_callable($labelTranslationParameters)
195+
? $labelTranslationParameters($choice, $key, $value)
196+
: (isset($labelTranslationParameters[$key]) ? $labelTranslationParameters[$key] : [])
190197
);
191198

192199
// $isPreferred may be null if no choices are preferred
@@ -198,7 +205,7 @@ private static function addChoiceView($choice, string $value, $label, array $key
198205
$otherViews[$nextIndex] = $view;
199206
}
200207

201-
private static function addChoiceViewsFromStructuredValues(array $values, $label, array $choices, array $keys, &$index, $attr, ?callable $isPreferred, array &$preferredViews, array &$preferredViewsOrder, array &$otherViews)
208+
private static function addChoiceViewsFromStructuredValues(array $values, $label, array $choices, array $keys, &$index, $attr, $labelTranslationParameters, ?callable $isPreferred, array &$preferredViews, array &$preferredViewsOrder, array &$otherViews)
202209
{
203210
foreach ($values as $key => $value) {
204211
if (null === $value) {
@@ -217,6 +224,7 @@ private static function addChoiceViewsFromStructuredValues(array $values, $label
217224
$keys,
218225
$index,
219226
$attr,
227+
$labelTranslationParameters,
220228
$isPreferred,
221229
$preferredViewsForGroup,
222230
$preferredViewsOrder,
@@ -242,6 +250,7 @@ private static function addChoiceViewsFromStructuredValues(array $values, $label
242250
$keys,
243251
$index,
244252
$attr,
253+
$labelTranslationParameters,
245254
$isPreferred,
246255
$preferredViews,
247256
$preferredViewsOrder,
@@ -250,7 +259,7 @@ private static function addChoiceViewsFromStructuredValues(array $values, $label
250259
}
251260
}
252261

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

@@ -263,6 +272,7 @@ private static function addChoiceViewsGroupedByCallable(callable $groupBy, $choi
263272
$keys,
264273
$index,
265274
$attr,
275+
$labelTranslationParameters,
266276
$isPreferred,
267277
$preferredViews,
268278
$preferredViewsOrder,
@@ -292,6 +302,7 @@ private static function addChoiceViewsGroupedByCallable(callable $groupBy, $choi
292302
$keys,
293303
$index,
294304
$attr,
305+
$labelTranslationParameters,
295306
$isPreferred,
296307
$preferredViews[$groupLabel]->choices,
297308
$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
+26-7Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -145,15 +145,16 @@ 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
{
158159
$accessor = $this->propertyAccessor;
159160

@@ -217,6 +218,24 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,
217218
};
218219
}
219220

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

0 commit comments

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