8000 [Form] Add choice_translation_parameters option for ChoiceType form by VincentLanglet · Pull Request #36851 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Form] Add choice_translation_parameters option for ChoiceType form #36851

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
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

8000
Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Make choice_translation_parameters similar to choice_attr and support…
… callable
  • Loading branch information
VincentLanglet committed Oct 4, 2020
commit 3c48b9aa1a567c503ce85c367dc9a7f7206ebf4f
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,8 @@
{% block choice_widget_expanded -%}
<div {{ block('widget_container_attributes') }}>
{%- for child in form %}
{# NEXT_MAJOR: Use default([]) for choice_translation_parameters #}
{{- form_widget(child, {
parent_label_class: label_attr.class|default(''),
label_translation_parameters: choice_translation_parameters|default(label_translation_parameters),
translation_domain: choice_translation_domain,
valid: valid,
}) -}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,20 +151,16 @@
{% block choice_widget_expanded -%}
{%- if '-inline' in label_attr.class|default('') -%}
{%- for child in form %}
{# NEXT_MAJOR: Use default([]) for choice_translation_parameters #}
{{- form_widget(child, {
parent_label_class: label_attr.class|default(''),
label_translation_parameters: choice_translation_parameters|default(label_translation_parameters),
translation_domain: choice_translation_domain,
}) -}}
{% endfor -%}
{%- else -%}
<div {{ block('widget_container_attributes') }}>
{%- for child in form %}
{# NEXT_MAJOR: Use default([]) for choice_translation_parameters #}
{{- form_widget(child, {
parent_label_class: label_attr.class|default(''),
label_translation_parameters: choice_translation_parameters|default(label_translation_parameters),
translation_domain: choice_translation_domain,
}) -}}
{%- endfor -%}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@
<div {{ block('widget_container_attributes') }}>
{%- for child in form %}
{{- form_widget(child) -}}
{# NEXT_MAJOR: Use default([]) for choice_translation_parameters #}
{{- form_label(child, null, {label_translation_parameters: choice_translation_parameters|default(label_translation_parameters), translation_domain: choice_translation_domain}) -}}
{{- form_label(child, null, {translation_domain: choice_translation_domain}) -}}
{% endfor -%}
</div>
{%- endblock choice_widget_expanded -%}
Expand Down Expand Up @@ -81,14 +80,12 @@
{%- block choice_widget_options -%}
{% for group_label, choice in options %}
{%- if choice is iterable -%}
{# NEXT_MAJOR: Use default([]) for choice_translation_parameters #}
<optgroup label="{{ choice_translation_domain is same as(false) ? group_label : group_label|trans(choice_translation_parameters|default(label_translation_parameters), choice_translation_domain) }}">
<optgroup label="{{ choice_translation_domain is same as(false) ? group_label : group_label|trans({}, choice_translation_domain) }}">
{% set options = choice %}
{{- block('choice_widget_options') -}}
</optgroup>
{%- else -%}
{# NEXT_MAJOR: Use default([]) for choice_translation_parameters #}
<option value="{{ choice.value }}"{% if choice.attr %}{% with { attr: choice.attr } %}{{ block('attributes') }}{% endwith %}{% endif %}{% if not render_preferred_choices|default(false) and choice is selectedchoice(value) %} selected="selected"{% endif %}>{{ choice_translation_domain is same as(false) ? choice.label : choice.label|trans(choice_translation_parameters|default(label_translation_parameters), choice_translation_domain) }}</option>
<option value="{{ choice.value }}"{% if choice.attr %}{% with { attr: choice.attr } %}{{ block('attributes') }}{% endwith %}{% endif %}{% if not render_preferred_choices|default(false) and choice is selectedchoice(value) %} selected="selected"{% endif %}>{{ choice_translation_domain is same as(false) ? choice.label : choice.label|trans(choice.labelTranslationParameters, choice_translation_domain) }}</option>
{%- endif -%}
{% endfor %}
{%- endblock choice_widget_options -%}
Expand Down
13 changes: 13 additions & 0 deletions src/Symfony/Component/Form/ChoiceList/ChoiceList.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\Form\ChoiceList\Factory\Cache\ChoiceFilter;
use Symfony\Component\Form\ChoiceList\Factory\Cache\ChoiceLabel;
use Symfony\Component\Form\ChoiceList\Factory\Cache\ChoiceLoader;
use Symfony\Component\Form\ChoiceList\Factory\Cache\ChoiceTranslationParameters;
use Symfony\Component\Form\ChoiceList\Factory\Cache\ChoiceValue;
use Symfony\Component\Form\ChoiceList\Factory\Cache\GroupBy;
use Symfony\Component\Form\ChoiceList\Factory\Cache\PreferredChoice;
Expand Down Expand Up @@ -113,6 +114,18 @@ public static function attr($formType, $attr, $vary = null): ChoiceAttr
return new ChoiceAttr($formType, $attr, $vary);
}

/**
* Decorates a "choice_translation_parameters" option to make it cacheable.
*
* @param FormTypeInterface|FormTypeExtensionInterface $formType A form type or type extension configuring a cacheable choice list
* @param callable|array $translationParameters Any pseudo callable or array to create translation parameters from a choice
* @param mixed|null $vary Dynamic data used to compute a unique hash when caching the option
*/
public static function translationParameters($formType, $translationParameters, $vary = null): ChoiceTranslationParameters
{
return new ChoiceTranslationParameters($formType, $translationParameters, $vary);
}

/**
* Decorates a "group_by" callback to make it cacheable.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Form\ChoiceList\Factory\Cache;

use Symfony\Component\Form\FormTypeExtensionInterface;
use Symfony\Component\Form\FormTypeInterface;

/**
* A cacheable wrapper for any {@see FormTypeInterface} or {@see FormTypeExtensionInterface}
* which configures a "choice_translation_parameters" option.
*
* @internal
*
* @author Vincent Langlet <vincentlanglet@users.noreply.github.com>
*/
final class ChoiceTranslationParameters extends AbstractStaticOption
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,14 @@ public function createListFromLoader(ChoiceLoaderInterface $loader, $value = nul
/**
* {@inheritdoc}
*
* @param array|callable|Cache\PreferredChoice|null $preferredChoices The preferred choices
* @param callable|false|Cache\ChoiceLabel|null $label The option or static option generating the choice labels
* @param callable|Cache\ChoiceFieldName|null $index The option or static option generating the view indices
* @param callable|Cache\GroupBy|null $groupBy The option or static option generating the group names
* @param array|callable|Cache\ChoiceAttr|null $attr The option or static option generating the HTML attributes
* @param array|callable|Cache\PreferredChoice|null $preferredChoices The preferred choices
* @param callable|false|Cache\ChoiceLabel|null $label The option or static option generating the choice labels
* @param callable|Cache\ChoiceFieldName|null $index The option or static option generating the view indices
* @param callable|Cache\GroupBy|null $groupBy The option or static option generating the group names
* @param array|callable|Cache\ChoiceAttr|null $attr The option or static option generating the HTML attributes
* @param array|callable|Cache\ChoiceTranslationParameters $labelTranslationParameters The parameters used to translate the choice labels
*/
public function createView(ChoiceListInterface $list, $preferredChoices = null, $label = null, $index = null, $groupBy = null, $attr = null)
public function createView(ChoiceListInterface $list, $preferredChoices = null, $label = null, $index = null, $groupBy = null, $attr = null, $labelTranslationParameters = [])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding the argument here is a BC break.

{
$cache = true;

Expand Down Expand Up @@ -214,11 +215,25 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,
$cache = false;
}

if ($labelTranslationParameters instanceof Cache\ChoiceTranslationParameters) {
$labelTranslationParameters = $labelTranslationParameters->getOption();
} elseif ([] !== $labelTranslationParameters) {
$cache = false;
}

if (!$cache) {
return $this->decoratedFactory->createView($list, $preferredChoices, $label, $index, $groupBy, $attr);
return $this->decoratedFactory->createView(
$list,
$preferredChoices,
$label,
$index,
$groupBy,
$attr,
$labelTranslationParameters
);
}

$hash = self::generateHash([$list, $preferredChoices, $label, $index, $groupBy, $attr]);
$hash = self::generateHash([$list, $preferredChoices, $label, $index, $groupBy, $attr, $labelTranslationParameters]);

if (!isset($this->views[$hash])) {
$this->views[$hash] = $this->decoratedFactory->createView(
Expand All @@ -227,7 +242,8 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,
$label,
$index,
$groupBy,
$attr
$attr,
$labelTranslationParameters
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,13 @@ public function createListFromLoader(ChoiceLoaderInterface $loader, callable $va
* match the keys of the choices. The values should be arrays of HTML
* attributes that should be added to the respective choice.
*
* @param array|callable|null $preferredChoices The preferred choices
* @param callable|false|null $label The callable generating the choice labels;
* pass false to discard the label
* @param array|callable|null $attr The callable generating the HTML attributes
* @param array|callable|null $preferredChoices The preferred choices
* @param callable|false|null $label The callable generating the choice labels;
* pass false to discard the label
* @param array|callable|null $attr The callable generating the HTML attributes
* @param array|callable $labelTranslationParameters The parameters used to translate the choice labels
*
* @return ChoiceListView The choice list view
*/
public function createView(ChoiceListInterface $list, $preferredChoices = null, $label = null, callable $index = null, callable $groupBy = null, $attr = null);
public function createView(ChoiceListInterface $list, $preferredChoices = null, $label = null, callable $index = null, callable $groupBy = null, $attr = null/*, $labelTranslationParameters = []*/);
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function createListFromLoader(ChoiceLoaderInterface $loader, callable $va
/**
* {@inheritdoc}
*/
public function createView(ChoiceListInterface $list, $preferredChoices = null, $label = null, callable $index = null, callable $groupBy = null, $attr = null)
public function createView(ChoiceListInterface $list, $preferredChoices = null, $label = null, callable $index = null, callable $groupBy = null, $attr = null, $labelTranslationParameters = [])
{
$preferredViews = [];
$preferredViewsOrder = [];
Expand Down Expand Up @@ -109,6 +109,7 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,
$keys,
$index,
$attr,
$labelTranslationParameters,
$preferredChoices,
$preferredViews,
$preferredViewsOrder,
Expand Down Expand Up @@ -146,6 +147,7 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,
$keys,
$index,
$attr,
$labelTranslationParameters,
$preferredChoices,
$preferredViews,
$preferredViewsOrder,
Expand All @@ -162,7 +164,7 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,
return new ChoiceListView($otherViews, $preferredViews);
}

private static function addChoiceView($choice, string $value, $label, array $keys, &$index, $attr, ?callable $isPreferred, array &$preferredViews, array &$preferredViewsOrder, array &$otherViews)
private static function addChoiceView($choice, string $value, $label, array $keys, &$index, $attr, $labelTranslationParameters, ?callable $isPreferred, array &$preferredViews, array &$preferredViewsOrder, array &$otherViews)
{
// $value may be an integer or a string, since it's stored in the array
// keys. We want to guarantee it's a string though.
Expand All @@ -186,7 +188,12 @@ private static function addChoiceView($choice, string $value, $label, array $key
$label,
// The attributes may be a callable or a mapping from choice indices
// to nested arrays
\is_callable($attr) ? $attr($choice, $key, $value) : (isset($attr[$key]) ? $attr[$key] : [])
\is_callable($attr) ? $attr($choice, $key, $value) : (isset($attr[$key]) ? $attr[$key] : []),
// The label translation parameters may be a callable or a mapping from choice indices
// to nested arrays
\is_callable($labelTranslationParameters)
? $labelTranslationParameters($choice, $key, $value)
: (isset($labelTranslationParameters[$key]) ? $labelTranslationParameters[$key] : [])
);

// $isPreferred may be null if no choices are preferred
Expand All @@ -198,7 +205,7 @@ private static function addChoiceView($choice, string $value, $label, array $key
$otherViews[$nextIndex] = $view;
}

private static function addChoiceViewsFromStructuredValues(array $values, $label, array $choices, array $keys, &$index, $attr, ?callable $isPreferred, array &$preferredViews, array &$preferredViewsOrder, array &$otherViews)
private static function addChoiceViewsFromStructuredValues(array $values, $label, array $choices, array $keys, &$index, $attr, $labelTranslationParameters, ?callable $isPreferred, array &$preferredViews, array &$preferredViewsOrder, array &$otherViews)
{
foreach ($values as $key => $value) {
if (null === $value) {
Expand All @@ -217,6 +224,7 @@ private static function addChoiceViewsFromStructuredValues(array $values, $label
$keys,
$index,
$attr,
$labelTranslationParameters,
$isPreferred,
$preferredViewsForGroup,
$preferredViewsOrder,
Expand All @@ -242,6 +250,7 @@ private static function addChoiceViewsFromStructuredValues(array $values, $label
$keys,
$index,
$attr,
$labelTranslationParameters,
$isPreferred,
$preferredViews,
$preferredViewsOrder,
Expand All @@ -250,7 +259,7 @@ private static function addChoiceViewsFromStructuredValues(array $values, $label
}
}

private static function addChoiceViewsGroupedByCallable(callable $groupBy, $choice, string $value, $label, array $keys, &$index, $attr, ?callable $isPreferred, array &$preferredViews, array &$preferredViewsOrder, array &$otherViews)
private static function addChoiceViewsGroupedByCallable(callable $groupBy, $choice, string $value, $label, array $keys, &$index, $attr, $labelTranslationParameters, ?callable $isPreferred, array &$preferredViews, array &$preferredViewsOrder, array &$otherViews)
{
$groupLabels = $groupBy($choice, $keys[$value], $value);

Expand All @@ -263,6 +272,7 @@ private static function addChoiceViewsGroupedByCallable(callable $groupBy, $choi
$keys,
$index,
$attr,
$labelTranslationParameters,
$isPreferred,
$preferredViews,
$preferredViewsOrder,
Expand Down Expand Up @@ -292,6 +302,7 @@ private static function addChoiceViewsGroupedByCallable(callable $groupBy, $choi
$keys,
$index,
$attr,
$labelTranslationParameters,
$isPreferred,
$preferredViews[$groupLabel]->choices,
$preferredViewsOrder[$groupLabel],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,16 @@ public function createListFromLoader(ChoiceLoaderInterface $loader, $value = nul
/**
* {@inheritdoc}
*
* @param array|callable|string|PropertyPath|null $preferredChoices The preferred choices
* @param callable|string|PropertyPath|null $label The callable or path generating the choice labels
* @param callable|string|PropertyPath|null $index The callable or path generating the view indices
* @param callable|string|PropertyPath|null $groupBy The callable or path generating the group names
* @param array|callable|string|PropertyPath|null $attr The callable or path generating the HTML attributes
* @param array|callable|string|PropertyPath|null $preferredChoices The preferred choices
* @param callable|string|PropertyPath|null $label The callable or path generating the choice labels
* @param callable|string|PropertyPath|null $index The callable or path generating the view indices
* @param callable|string|PropertyPath|null $groupBy The callable or path generating the group names
* @param array|callable|string|PropertyPath|null $attr The callable or path generating the HTML attributes
* @param array|callable|string|PropertyPath $labelTranslationParameters The callable or path generating the parameters used to translate the choice labels
*
* @return ChoiceListView The choice list view
*/
public function createView(ChoiceListInterface $list, $preferredChoices = null, $label = null, $index = null, $groupBy = null, $attr = null)
public function createView(ChoiceListInterface $list, $preferredChoices = null, $label = null, $index = null, $groupBy = null, $attr = null, $labelTranslationParameters = [])
{
$accessor = $this->propertyAccessor;

Expand Down Expand Up @@ -217,6 +218,24 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,
};
}

return $this->decoratedFactory->createView($list, $preferredChoices, $label, $index, $groupBy, $attr);
if (\is_string($labelTranslationParameters)) {
$labelTranslationParameters = new PropertyPath($labelTranslationParameters);
}

if ($labelTranslationParameters instanceof PropertyPath) {
$labelTranslationParameters = function ($choice) use ($accessor, $labelTranslationParameters) {
return $accessor->getValue($choice, $labelTranslationParameters);
};
}

return $this->decoratedFactory->createView(
$list,
$preferredChoices,
$label,
$index,
$groupBy,
$attr,
$labelTranslationParameters
);
}
}
Loading
0