Skip to content

Navigation Menu

Sign in
Appearance settings

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 1ea5346

Browse filesBrowse files
Make choice_translation_parameters similar to choice_attr and support callable
1 parent 57258a0 commit 1ea5346
Copy full SHA for 1ea5346

File tree

Expand file treeCollapse file tree

8 files changed

+77
-25
lines changed
Filter options
Expand file treeCollapse file tree

8 files changed

+77
-25
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.label_translation_parameters, 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/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 = null)
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/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
}

‎src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php
+6-3Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\Form\ChoiceList\Factory\Cache\ChoiceFilter;
1919
use Symfony\Component\Form\ChoiceList\Factory\Cache\ChoiceLabel;
2020
use Symfony\Component\Form\ChoiceList\Factory\Cache\ChoiceLoader;
21+
use Symfony\Component\Form\ChoiceList\Factory\Cache\ChoiceTranslationParameters;
2122
use Symfony\Component\Form\ChoiceList\Factory\Cache\ChoiceValue;
2223
use Symfony\Component\Form\ChoiceList\Factory\Cache\GroupBy;
2324
use Symfony\Component\Form\ChoiceList\Factory\Cache\PreferredChoice;
@@ -211,7 +212,6 @@ public function buildView(FormView $view, FormInterface $form, array $options)
211212
'choices' => $choiceListView->choices,
212213
'separator' => '-------------------',
213214
'placeholder' => null,
214-
'choice_translation_parameters' => $options['choice_translation_parameters'],
215215
'choice_translation_domain' => $choiceTranslationDomain,
216216
]);
217217

@@ -327,6 +327,7 @@ public function configureOptions(OptionsResolver $resolver)
327327
'choice_name' => null,
328328
'choice_value' => null,
329329
'choice_attr' => null,
330+
'choice_translation_parameters' => null,
330331
'preferred_choices' => [],
331332
'group_by' => null,
332333
'empty_data' => $emptyData,
@@ -337,7 +338,6 @@ public function configureOptions(OptionsResolver $resolver)
337338
// even if the "data" option is manually set to an object.
338339
// See https://github.com/symfony/symfony/pull/5582
339340
'data_class' => null,
340-
'choice_translation_parameters' => [],
341341
'choice_translation_domain' => true,
342342
'trim' => false,
343343
]);
@@ -353,6 +353,7 @@ public function configureOptions(OptionsResolver $resolver)
353353
$resolver->setAllowedTypes('choice_name', ['null', 'callable', 'string', 'Symfony\Component\PropertyAccess\PropertyPath', ChoiceFieldName::class]);
354354
$resolver->setAllowedTypes('choice_value', ['null', 'callable', 'string', 'Symfony\Component\PropertyAccess\PropertyPath', ChoiceValue::class]);
355355
$resolver->setAllowedTypes('choice_attr', ['null', 'array', 'callable', 'string', 'Symfony\Component\PropertyAccess\PropertyPath', ChoiceAttr::class]);
356+
$resolver->setAllowedTypes('choice_translation_parameters', ['null', 'array', 'callable', ChoiceTranslationParameters::class]);
356357
$resolver->setAllowedTypes('preferred_choices', ['array', '\Traversable', 'callable', 'string', 'Symfony\Component\PropertyAccess\PropertyPath', PreferredChoice::class]);
357358
$resolver->setAllowedTypes('group_by', ['null', 'callable', 'string', 'Symfony\Component\PropertyAccess\PropertyPath', GroupBy::class]);
358359
}
@@ -392,6 +393,7 @@ private function addSubForm(FormBuilderInterface $builder, string $name, ChoiceV
392393
'value' => $choiceView->value,
393394
'label' => $choiceView->label,
394395
'attr' => $choiceView->attr,
396+
'label_translation_parameters' => $choiceView->labelTranslationParameters,
395397
'translation_domain' => $options['translation_domain'],
396398
'block_name' => 'entry',
397399
];
@@ -436,7 +438,8 @@ private function createChoiceListView(ChoiceListInterface $choiceList, array $op
436438
$options['choice_label'],
437439
$options['choice_name'],
438440
$options['group_by'],
439-
$options['choice_attr']
441+
$options['choice_attr'],
442+
$options['choice_translation_domain']
440443
);
441444
}
442445
}

0 commit comments

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