diff --git a/src/Symfony/Component/Form/CHANGELOG.md b/src/Symfony/Component/Form/CHANGELOG.md index f2f949c897110..588408aaf4209 100644 --- a/src/Symfony/Component/Form/CHANGELOG.md +++ b/src/Symfony/Component/Form/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +6.2 +--- + +* Allow passing `TranslatableInterface` objects to the `ChoiceView` label + 6.1 --- diff --git a/src/Symfony/Component/Form/ChoiceList/Factory/DefaultChoiceListFactory.php b/src/Symfony/Component/Form/ChoiceList/Factory/DefaultChoiceListFactory.php index e22e81f235bf2..cee3e2840a73c 100644 --- a/src/Symfony/Component/Form/ChoiceList/Factory/DefaultChoiceListFactory.php +++ b/src/Symfony/Component/Form/ChoiceList/Factory/DefaultChoiceListFactory.php @@ -20,7 +20,7 @@ use Symfony\Component\Form\ChoiceList\View\ChoiceGroupView; use Symfony\Component\Form\ChoiceList\View\ChoiceListView; use Symfony\Component\Form\ChoiceList\View\ChoiceView; -use Symfony\Component\Translation\TranslatableMessage; +use Symfony\Contracts\Translation\TranslatableInterface; /** * Default implementation of {@link ChoiceListFactoryInterface}. @@ -175,7 +175,7 @@ private static function addChoiceView($choice, string $value, $label, array $key if (false === $dynamicLabel) { $label = false; - } elseif ($dynamicLabel instanceof TranslatableMessage) { + } elseif ($dynamicLabel instanceof TranslatableInterface) { $label = $dynamicLabel; } else { $label = (string) $dynamicLabel; diff --git a/src/Symfony/Component/Form/ChoiceList/View/ChoiceView.php b/src/Symfony/Component/Form/ChoiceList/View/ChoiceView.php index a98dd79541944..050d8ed243fda 100644 --- a/src/Symfony/Component/Form/ChoiceList/View/ChoiceView.php +++ b/src/Symfony/Component/Form/ChoiceList/View/ChoiceView.php @@ -11,7 +11,7 @@ namespace Symfony\Component\Form\ChoiceList\View; -use Symfony\Component\Translation\TranslatableMessage; +use Symfony\Contracts\Translation\TranslatableInterface; /** * Represents a choice in templates. @@ -37,13 +37,13 @@ class ChoiceView /** * Creates a new choice view. * - * @param mixed $data The original choice - * @param string $value The view representation of the choice - * @param string|TranslatableMessage|false $label The label displayed to humans; pass false to discard the label - * @param array $attr Additional attributes for the HTML tag - * @param array $labelTranslationParameters Additional parameters used to translate the label + * @param mixed $data The original choice + * @param string $value The view representation of the choice + * @param string|TranslatableInterface|false $label The label displayed to humans; pass false to discard the label + * @param array $attr Additional attributes for the HTML tag + * @param array $labelTranslationParameters Additional parameters used to translate the label */ - public function __construct(mixed $data, string $value, string|TranslatableMessage|false $label, array $attr = [], array $labelTranslationParameters = []) + public function __construct(mixed $data, string $value, string|TranslatableInterface|false $label, array $attr = [], array $labelTranslationParameters = []) { $this->data = $data; $this->value = $value; diff --git a/src/Symfony/Component/Form/Tests/ChoiceList/Factory/DefaultChoiceListFactoryTest.php b/src/Symfony/Component/Form/Tests/ChoiceList/Factory/DefaultChoiceListFactoryTest.php index 767662f698648..ab26d4cbe3ded 100644 --- a/src/Symfony/Component/Form/Tests/ChoiceList/Factory/DefaultChoiceListFactoryTest.php +++ b/src/Symfony/Component/Form/Tests/ChoiceList/Factory/DefaultChoiceListFactoryTest.php @@ -22,6 +22,8 @@ use Symfony\Component\Form\ChoiceList\View\ChoiceView; use Symfony\Component\Form\Tests\Fixtures\ArrayChoiceLoader; use Symfony\Component\Translation\TranslatableMessage; +use Symfony\Contracts\Translation\TranslatableInterface; +use Symfony\Contracts\Translation\TranslatorInterface; class DefaultChoiceListFactoryTest extends TestCase { @@ -774,6 +776,26 @@ static function ($choice, $key, $value) { $this->assertArrayHasKey('param1', $view->choices[0]->label->getParameters()); } + public function testPassTranslatableInterfaceAsLabelDoesntCastItToString() + { + $message = new class() implements TranslatableInterface { + public function trans(TranslatorInterface $translator, string $locale = null): string + { + return 'my_message'; + } + }; + + $view = $this->factory->createView( + $this->list, + [$this->obj1], + static function () use ($message) { + return $message; + } + ); + + $this->assertSame($message, $view->choices[0]->label); + } + public function testCreateViewFlatLabelTranslationParametersAsArray() { $view = $this->factory->createView(