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 abcbbf2

Browse filesBrowse files
committed
feature #46946 [Form] ChoiceType choices must support TranslatableInterface (alamirault)
This PR was merged into the 6.2 branch. Discussion ---------- [Form] ChoiceType choices must support TranslatableInterface | Q | A | ------------- | --- | Branch? | 6.2 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | Fix #46902 | License | MIT | Doc PR | Before this PR only `TranslatableMessage` was supported in ChoiceView/ChoiceType choices labels. Now `TranslatableInterface` contract is supported. Commits ------- cffb4df [Form] ChoiceView label allow TranslatableInterface, not only TranslatableMessage
2 parents 97ef78d + cffb4df commit abcbbf2
Copy full SHA for abcbbf2

File tree

Expand file treeCollapse file tree

4 files changed

+36
-9
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+36
-9
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/CHANGELOG.md
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
6.2
5+
---
6+
7+
* Allow passing `TranslatableInterface` objects to the `ChoiceView` label
8+
49
6.1
510
---
611

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/ChoiceList/Factory/DefaultChoiceListFactory.php
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
use Symfony\Component\Form\ChoiceList\View\ChoiceGroupView;
2121
use Symfony\Component\Form\ChoiceList\View\ChoiceListView;
2222
use Symfony\Component\Form\ChoiceList\View\ChoiceView;
23-
use Symfony\Component\Translation\TranslatableMessage;
23+
use Symfony\Contracts\Translation\TranslatableInterface;
2424

2525
/**
2626
* Default implementation of {@link ChoiceListFactoryInterface}.
@@ -175,7 +175,7 @@ private static function addChoiceView($choice, string $value, $label, array $key
175175

176176
if (false === $dynamicLabel) {
177177
$label = false;
178-
} elseif ($dynamicLabel instanceof TranslatableMessage) {
178+
} elseif ($dynamicLabel instanceof TranslatableInterface) {
179179
$label = $dynamicLabel;
180180
} else {
181181
$label = (string) $dynamicLabel;

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/ChoiceList/View/ChoiceView.php
+7-7Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace Symfony\Component\Form\ChoiceList\View;
1313

14-
use Symfony\Component\Translation\TranslatableMessage;
14+
use Symfony\Contracts\Translation\TranslatableInterface;
1515

1616
/**
1717
* Represents a choice in templates.
@@ -37,13 +37,13 @@ class ChoiceView
3737
/**
3838
* Creates a new choice view.
3939
*
40-
* @param mixed $data The original choice
41-
* @param string $value The view representation of the choice
42-
* @param string|TranslatableMessage|false $label The label displayed to humans; pass false to discard the label
43-
* @param array $attr Additional attributes for the HTML tag
44-
* @param array $labelTranslationParameters Additional parameters used to translate the label
40+
* @param mixed $data The original choice
41+
* @param string $value The view representation of the choice
42+
* @param string|TranslatableInterface|false $label The label displayed to humans; pass false to discard the label
43+
* @param array $attr Additional attributes for the HTML tag
44+
* @param array $labelTranslationParameters Additional parameters used to translate the label
4545
*/
46-
public function __construct(mixed $data, string $value, string|TranslatableMessage|false $label, array $attr = [], array $labelTranslationParameters = [])
46+
public function __construct(mixed $data, string $value, string|TranslatableInterface|false $label, array $attr = [], array $labelTranslationParameters = [])
4747
{
4848
$this->data = $data;
4949
$this->value = $value;

‎src/Symfony/Component/Form/Tests/ChoiceList/Factory/DefaultChoiceListFactoryTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Tests/ChoiceList/Factory/DefaultChoiceListFactoryTest.php
+22Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
use Symfony\Component\Form\ChoiceList\View\ChoiceView;
2323
use Symfony\Component\Form\Tests\Fixtures\ArrayChoiceLoader;
2424
use Symfony\Component\Translation\TranslatableMessage;
25+
use Symfony\Contracts\Translation\TranslatableInterface;
26+
use Symfony\Contracts\Translation\TranslatorInterface;
2527

2628
class DefaultChoiceListFactoryTest extends TestCase
2729
{
@@ -774,6 +776,26 @@ static function ($choice, $key, $value) {
774776
$this->assertArrayHasKey('param1', $view->choices[0]->label->getParameters());
775777
}
776778

779+
public function testPassTranslatableInterfaceAsLabelDoesntCastItToString()
780+
{
781+
$message = new class() implements TranslatableInterface {
782+
public function trans(TranslatorInterface $translator, string $locale = null): string
783+
{
784+
return 'my_message';
785+
}
786+
};
787+
788+
$view = $this->factory->createView(
789+
$this->list,
790+
[$this->obj1],
791+
static function () use ($message) {
792+
return $message;
793+
}
794+
);
795+
796+
$this->assertSame($message, $view->choices[0]->label);
797+
}
798+
777799
public function testCreateViewFlatLabelTranslationParametersAsArray()
778800
{
779801
$view = $this->factory->createView(

0 commit comments

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