Skip to content

Commit 37166f7

Browse files
bug #40018 [TwigBridge] take into account all label related options (xabbuh)
This PR was merged into the 5.2 branch. Discussion ---------- [TwigBridge] take into account all label related options | Q | A | ------------- | --- | Branch? | 5.2 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #40007 | License | MIT | Doc PR | Commits ------- adb0fd1 take into account all label related options
2 parents b9094f4 + adb0fd1 commit 37166f7

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/Symfony/Bridge/Twig/Extension/FormExtension.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,18 @@ public function getFieldValue(FormView $view)
113113

114114
public function getFieldLabel(FormView $view): ?string
115115
{
116+
if (false === $label = $view->vars['label']) {
117+
return null;
118+
}
119+
120+
if (!$label && $labelFormat = $view->vars['label_format']) {
121+
$label = str_replace(['%id%', '%name%'], [$view->vars['id'], $view->vars['name']], $labelFormat);
122+
} elseif (!$label) {
123+
$label = ucfirst(strtolower(trim(preg_replace(['/([A-Z])/', '/[_\s]+/'], ['_$1', ' '], $view->vars['name']))));
124+
}
125+
116126
return $this->createFieldTranslation(
117-
$view->vars['label'],
127+
$label,
118128
$view->vars['label_translation_parameters'] ?: [],
119129
$view->vars['translation_domain']
120130
);

src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionFieldHelpersTest.php

+17
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ protected function setUp(): void
8181
],
8282
],
8383
'choice_translation_domain' => 'forms',
84+
'label_format' => 'label format for field "%name%" with id "%id%"',
8485
])
8586
->add('choice_multiple', ChoiceType::class, [
8687
'choices' => [
@@ -89,6 +90,7 @@ protected function setUp(): void
8990
],
9091
'multiple' => true,
9192
'expanded' => true,
93+
'label' => false,
9294
])
9395
->getForm()
9496
;
@@ -121,6 +123,21 @@ public function testFieldTranslatedLabel()
121123
$this->assertSame('[trans]base.username[/trans]', $this->translatorExtension->getFieldLabel($this->view->children['username']));
122124
}
123125

126+
public function testFieldLabelFromFormat()
127+
{
128+
$this->assertSame('label format for field "choice_grouped" with id "register_choice_grouped"', $this->rawExtension->getFieldLabel($this->view->children['choice_grouped']));
129+
}
130+
131+
public function testFieldLabelFallsBackToName()
132+
{
133+
$this->assertSame('Choice flat', $this->rawExtension->getFieldLabel($this->view->children['choice_flat']));
134+
}
135+
136+
public function testFieldLabelReturnsNullWhenLabelIsDisabled()
137+
{
138+
$this->assertNull($this->rawExtension->getFieldLabel($this->view->children['choice_multiple']));
139+
}
140+
124141
public function testFieldHelp()
125142
{
126143
$this->assertSame('base.username_help', $this->rawExtension->getFieldHelp($this->view->children['username']));

0 commit comments

Comments
 (0)