Skip to content

[Form] Add choice_translation_parameters option #13677

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions reference/forms/types/choice.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ To use this field, you must specify *either* ``choices`` or ``choice_loader`` op
| | - `choice_label`_ |
| | - `choice_loader`_ |
| | - `choice_name`_ |
| | - `choice_translation_parameters`_ |
| | - `choice_translation_domain`_ |
| | - `choice_value`_ |
| | - `expanded`_ |
Expand Down Expand Up @@ -225,6 +226,8 @@ correct types will be assigned to the model.

.. include:: /reference/forms/types/options/choice_name.rst.inc

.. include:: /reference/forms/types/options/choice_translation_parameters.rst.inc

.. include:: /reference/forms/types/options/choice_translation_domain.rst.inc

.. include:: /reference/forms/types/options/choice_value.rst.inc
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
choice_translation_parameters
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

**type**: ``array``, ``callable``, ``string`` or :class:`Symfony\\Component\\PropertyAccess\\PropertyPath` **default**: ``[]``

The choice values are translated before displaying it, so it can contain
:ref:`translation placeholders <component-translation-placeholders>`.
This option defines the values used to replace those placeholders. This can be
an associative array where the keys match the choice keys and the values
are the attributes for each choice, a callable or a property path
(just like `choice_label`_).

Given this translation message:

.. code-block:: yaml

# translations/messages.en.yaml
form.order.yes: 'I confirm my order to the company %company%'
form.order.no: 'I cancel my order'

You can specify the placeholder values as follows::

$builder->add('id', null, [
'choice' => [
'form.order.yes' => true,
'form.order.no' => false,
],
'choice_translation_parameters' => function ($choice, $key, $value) {
if (false === $choice) {
return [];
}

return ['%company%' => 'ACME Inc.']
},
]);

If an array, the keys of the ``choices`` array must be used as keys::

$builder->add('id', null, [
'choice' => [
'form.order.yes' => true,
'form.order.no' => false,
],
'choice_translation_parameters' => [
'form.order.yes' => ['%company%' => 'ACME Inc.'],
'form.order.no' => [],
],
]);

The ``choice_translation_parameters`` option of children fields is merged with
the same option of their parents, so children can reuse and/or override any of
the parent placeholders.