Skip to content

Commit 1dd2007

Browse files
webnet-frfabpot
authored andcommitted
[Form] Add label_translation_parameters, help_translation_parameters and attr_translation_parameters options to base form type
1 parent fad93db commit 1dd2007

File tree

10 files changed

+331
-61
lines changed

10 files changed

+331
-61
lines changed

CHANGELOG.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,30 @@ CHANGELOG
88
option is set to `single_text`
99
* added `block_prefix` option to `BaseType`.
1010
* added `help_html` option to display the `help` text as HTML.
11+
* added `label_translation_parameters`, `attr_translation_parameters`, `help_translation_parameters` options
12+
to `FormType` to pass translation parameters to form labels, attributes (`placeholder` and `title`) and help text respectively.
13+
The passed parameters will replace placeholders in translation messages.
14+
15+
```php
16+
class OrderType extends AbstractType
17+
{
18+
public function buildForm(FormBuilderInterface $builder, array $options)
19+
{
20+
$builder->add('comment', TextType::class, [
21+
'label' => 'Comment to the order to %company%',
22+
'label_translation_parameters' => [
23+
'%company%' => 'Acme',
24+
],
25+
'help' => 'The address of the %company% is %address%',
26+
'help_translation_parameters' => [
27+
'%company%' => 'Acme Ltd.',
28+
'%address%' => '4 Form street, Symfonyville',
29+
],
30+
])
31+
}
32+
}
33+
```
34+
1135

1236
4.2.0
1337
-----

Extension/Core/Type/BaseType.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ public function buildView(FormView $view, FormInterface $form, array $options)
4444
$name = $form->getName();
4545
$blockName = $options['block_name'] ?: $form->getName();
4646
$translationDomain = $options['translation_domain'];
47+
$labelTranslationParameters = $options['label_translation_parameters'];
48+
$attrTranslationParameters = $options['attr_translation_parameters'];
4749
$labelFormat = $options['label_format'];
4850

4951
if ($view->parent) {
@@ -61,6 +63,9 @@ public function buildView(FormView $view, FormInterface $form, array $options)
6163
$translationDomain = $view->parent->vars['translation_domain'];
6264
}
6365

66+
$labelTranslationParameters = array_merge($view->parent->vars['label_translation_parameters'], $labelTranslationParameters);
67+
$attrTranslationParameters = array_merge($view->parent->vars['attr_translation_parameters'], $attrTranslationParameters);
68+
6469
if (!$labelFormat) {
6570
$labelFormat = $view->parent->vars['label_format'];
6671
}
@@ -97,6 +102,8 @@ public function buildView(FormView $view, FormInterface $form, array $options)
97102
'block_prefixes' => $blockPrefixes,
98103
'unique_block_prefix' => $uniqueBlockPrefix,
99104
'translation_domain' => $translationDomain,
105+
'label_translation_parameters' => $labelTranslationParameters,
106+
'attr_translation_parameters' => $attrTranslationParameters,
100107
// Using the block name here speeds up performance in collection
101108
// forms, where each entry has the same full block name.
102109
// Including the type is important too, because if rows of a
@@ -118,6 +125,8 @@ public function configureOptions(OptionsResolver $resolver)
118125
'disabled' => false,
119126
'label' => null,
120127
'label_format' => null,
128+
'label_translation_parameters' => [],
129+
'attr_translation_parameters' => [],
121130
'attr' => [],
122131
'translation_domain' => null,
123132
'auto_initialize' => true,

Extension/Core/Type/FormType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public function buildView(FormView $view, FormInterface $form, array $options)
6868
parent::buildView($view, $form, $options);
6969

7070
$name = $form->getName();
71+
$helpTranslationParameters = $options['help_translation_parameters'];
7172

7273
if ($view->parent) {
7374
if ('' === $name) {
@@ -78,6 +79,8 @@ public function buildView(FormView $view, FormInterface $form, array $options)
7879
if (!isset($view->vars['attr']['readonly']) && isset($view->parent->vars['attr']['readonly']) && false !== $view->parent->vars['attr']['readonly']) {
7980
$view->vars['attr']['readonly'] = true;
8081
}
82+
83+
$helpTranslationParameters = array_merge($view->parent->vars['help_translation_parameters'], $helpTranslationParameters);
8184
}
8285

8386
$formConfig = $form->getConfig();
@@ -92,6 +95,7 @@ public function buildView(FormView $view, FormInterface $form, array $options)
9295
'help' => $options['help'],
9396
'help_attr' => $options['help_attr'],
9497
'help_html' => $options['help_html'],
98+
'help_translation_parameters' => $helpTranslationParameters,
9599
'compound' => $formConfig->getCompound(),
96100
'method' => $formConfig->getMethod(),
97101
'action' => $formConfig->getAction(),
@@ -185,6 +189,7 @@ public function configureOptions(OptionsResolver $resolver)
185189
'help' => null,
186190
'help_attr' => [],
187191
'help_html' => false,
192+
'help_translation_parameters' => [],
188193
]);
189194

190195
$resolver->setAllowedTypes('label_attr', 'array');

Tests/AbstractLayoutTest.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2572,6 +2572,82 @@ public function testColor()
25722572
[@type="color"]
25732573
[@name="name"]
25742574
[@value="#0000ff"]
2575+
'
2576+
);
2577+
}
2578+
2579+
public function testLabelWithTranslationParameters()
2580+
{
2581+
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\TextType');
2582+
$html = $this->renderLabel($form->createView(), 'Address is %address%', [
2583+
'label_translation_parameters' => [
2584+
'%address%' => 'Paris, rue de la Paix',
2585+
],
2586+
]);
2587+
2588+
$this->assertMatchesXpath($html,
2589+
'/label
2590+
[@for="name"]
2591+
[.="[trans]Address is Paris, rue de la Paix[/trans]"]
2592+
'
2593+
);
2594+
}
2595+
2596+
public function testHelpWithTranslationParameters()
2597+
{
2598+
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, [
2599+
'help' => 'for company %company%',
2600+
'help_translation_parameters' => [
2601+
'%company%' => 'ACME Ltd.',
2602+
],
2603+
]);
2604+
$html = $this->renderHelp($form->createView());
2605+
2606+
$this->assertMatchesXpath($html,
2607+
'/*
2608+
[@id="name_help"]
2609+
[.="[trans]for company ACME Ltd.[/trans]"]
2610+
'
2611+
);
2612+
}
2613+
2614+
public function testAttributesWithTranslationParameters()
2615+
{
2616+
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, [
2617+
'attr' => [
2618+
'title' => 'Message to %company%',
2619+
'placeholder' => 'Enter a message to %company%',
2620+
],
2621+
'attr_translation_parameters' => [
2622+
'%company%' => 'ACME Ltd.',
2623+
],
2624+
]);
2625+
$html = $this->renderWidget($form->createView());
2626+
2627+
$this->assertMatchesXpath($html,
2628+
'/input
2629+
[@title="[trans]Message to ACME Ltd.[/trans]"]
2630+
[@placeholder="[trans]Enter a message to ACME Ltd.[/trans]"]
2631+
'
2632+
);
2633+
}
2634+
2635+
public function testButtonWithTranslationParameters()
2636+
{
2637+
$form = $this->factory->createNamedBuilder('myform')
2638+
->add('mybutton', 'Symfony\Component\Form\Extension\Core\Type\ButtonType', [
2639+
'label' => 'Submit to %company%',
2640+
'label_translation_parameters' => [
2641+
'%company%' => 'ACME Ltd.',
2642+
],
2643+
])
2644+
->getForm();
2645+
$view = $form->get('mybutton')->createView();
2646+
$html = $this->renderWidget($view, ['label_format' => 'form.%name%']);
2647+
2648+
$this->assertMatchesXpath($html,
2649+
'/button
2650+
[.="[trans]Submit to ACME Ltd.[/trans]"]
25752651
'
25762652
);
25772653
}

Tests/Extension/Core/Type/BaseTypeTest.php

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,102 @@ public function testDefaultTranslationDomain()
119119
$this->assertNull($view['child']->vars['translation_domain']);
120120
}
121121

122+
public function testPassLabelTranslationParametersToView()
123+
{
124+
$view = $this->factory->create($this->getTestedType(), null, [
125+
'label_translation_parameters' => ['%param%' => 'value'],
126+
])
127+
->createView();
128+
129+
$this->assertSame(['%param%' => 'value'], $view->vars['label_translation_parameters']);
130+
}
131+
132+
public function testPassAttrTranslationParametersToView()
133+
{
134+
$view = $this->factory->create($this->getTestedType(), null, [
135+
'attr_translation_parameters' => ['%param%' => 'value'],
136+
])
137+
->createView();
138+
139+
$this->assertSame(['%param%' => 'value'], $view->vars['attr_translation_parameters']);
140+
}
141+
142+
public function testInheritLabelTranslationParametersFromParent()
143+
{
144+
$view = $this->factory
145+
->createNamedBuilder('parent', FormTypeTest::TESTED_TYPE, null, [
146+
'label_translation_parameters' => ['%param%' => 'value'],
147+
])
148+
->add('child', $this->getTestedType())
149+
->getForm()
150+
->createView();
151+
152+
$this->assertEquals(['%param%' => 'value'], $view['child']->vars['label_translation_parameters']);
153+
}
154+
155+
public function testInheritAttrTranslationParametersFromParent()
156+
{
157+
$view = $this->factory
158+
->createNamedBuilder('parent', FormTypeTest::TESTED_TYPE, null, [
159+
'attr_translation_parameters' => ['%param%' => 'value'],
160+
])
161+
->add('child', $this->getTestedType())
162+
->getForm()
163+
->createView();
164+
165+
$this->assertEquals(['%param%' => 'value'], $view['child']->vars['attr_translation_parameters']);
166+
}
167+
168+
public function testPreferOwnLabelTranslationParameters()
169+
{
170+
$view = $this->factory
171+
->createNamedBuilder('parent', FormTypeTest::TESTED_TYPE, null, [
172+
'label_translation_parameters' => ['%parent_param%' => 'parent_value', '%override_param%' => 'parent_override_value'],
173+
])
174+
->add('child', $this->getTestedType(), [
175+
'label_translation_parameters' => ['%override_param%' => 'child_value'],
176+
])
177+
->getForm()
178+
->createView();
179+
180+
$this->assertEquals(['%parent_param%' => 'parent_value', '%override_param%' => 'child_value'], $view['child']->vars['label_translation_parameters']);
181+
}
182+
183+
public function testPreferOwnAttrTranslationParameters()
184+
{
185+
$view = $this->factory
186+
->createNamedBuilder('parent', FormTypeTest::TESTED_TYPE, null, [
187+
'attr_translation_parameters' => ['%parent_param%' => 'parent_value', '%override_param%' => 'parent_override_value'],
188+
])
189+
->add('child', $this->getTestedType(), [
190+
'attr_translation_parameters' => ['%override_param%' => 'child_value'],
191+
])
192+
->getForm()
193+
->createView();
194+
195+
$this->assertEquals(['%parent_param%' => 'parent_value', '%override_param%' => 'child_value'], $view['child']->vars['attr_translation_parameters']);
196+
}
197+
198+
public function testDefaultLabelTranslationParameters()
199+
{
200+
$view = $this->factory->createNamedBuilder('parent', FormTypeTest::TESTED_TYPE)
201+
->add('child', $this->getTestedType())
202+
->getForm()
203+
->createView();
204+
205+
$this->assertEquals([], $view['child']->vars['label_translation_parameters']);
206+
}
207+
208+
public function testDefaultAttrTranslationParameters()
209+
{
210+
$view = $this->factory->createNamedBuilder('parent', FormTypeTest::TESTED_TYPE)
211+
->add('child', $this->getTestedType())
212+
->getForm()
213+
->createView();
214+
215+
$this->assertEquals([], $view['child']->vars['attr_translation_parameters']);
216+
}
217+
122218
public function testPassLabelToView()
123219
{
124220
$view = $this->factory->createNamed('__test___field', $this->getTestedType(), null, ['label' => 'My label'])

Tests/Extension/Core/Type/FormTypeTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,4 +651,52 @@ public function testPassBlockPrefixToViewWithParent()
651651

652652
$this->assertSame(['form', 'child', '_parent_child'], $view['child']->vars['block_prefixes']);
653653
}
654+
655+
public function testDefaultHelpTranslationParameters()
656+
{
657+
$view = $this->factory->createNamedBuilder('parent', self::TESTED_TYPE)
658+
->add('child', $this->getTestedType())
659+
->getForm()
660+
->createView();
661+
662+
$this->assertEquals([], $view['child']->vars['help_translation_parameters']);
663+
}
664+
665+
public function testPassHelpTranslationParametersToView()
666+
{
667+
$view = $this->factory->create($this->getTestedType(), null, [
668+
'help_translation_parameters' => ['%param%' => 'value'],
669+
])
670+
->createView();
671+
672+
$this->assertSame(['%param%' => 'value'], $view->vars['help_translation_parameters']);
673+
}
674+
675+
public function testInheritHelpTranslationParametersFromParent()
676+
{
677+
$view = $this->factory
678+
->createNamedBuilder('parent', self::TESTED_TYPE, null, [
679+
'help_translation_parameters' => ['%param%' => 'value'],
680+
])
681+
->add('child', $this->getTestedType())
682+
->getForm()
683+
->createView();
684+
685+
$this->assertEquals(['%param%' => 'value'], $view['child']->vars['help_translation_parameters']);
686+
}
687+
688+
public function testPreferOwnHelpTranslationParameters()
689+
{
690+
$view = $this->factory
691+
->createNamedBuilder('parent', self::TESTED_TYPE, null, [
692+
'help_translation_parameters' => ['%parent_param%' => 'parent_value', '%override_param%' => 'parent_override_value'],
693+
])
694+
->add('child', $this->getTestedType(), [
695+
'help_translation_parameters' => ['%override_param%' => 'child_value'],
696+
])
697+
->getForm()
698+
->createView();
699+
700+
$this->assertEquals(['%parent_param%' => 'parent_value', '%override_param%' => 'child_value'], $view['child']->vars['help_translation_parameters']);
701+
}
654702
}

Tests/Fixtures/Descriptor/resolved_form_type_1.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"action",
3131
"allow_file_upload",
3232
"attr",
33+
"attr_translation_parameters",
3334
"auto_initialize",
3435
"block_name",
3536
"block_prefix",
@@ -39,10 +40,12 @@
3940
"help",
4041
"help_attr",
4142
"help_html",
43+
"help_translation_parameters",
4244
"inherit_data",
4345
"label",
4446
"label_attr",
4547
"label_format",
48+
"label_translation_parameters",
4649
"mapped",
4750
"method",
4851
"post_max_size_message",

0 commit comments

Comments
 (0)