-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[2.7][Form][choice] added choice_translation_domain to avoid trans options. #13651
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
8000
|
@@ -2,12 +2,12 @@ | |
|
||
$translatorHelper = $view['translator']; // outside of the loop for performance reasons! ?> | ||
<?php $formHelper = $view['form']; ?> | ||
<?php foreach ($choices as $index => $choice): ?> | ||
<?php foreach ($choices as $group_label => $choice): ?> | ||
<?php if (is_array($choice) || $choice instanceof ChoiceGroupView): ?> | ||
<optgroup label="<?php echo $view->escape($translatorHelper->trans($index, array(), $translation_domain)) ?>"> | ||
<optgroup label="<?php echo $view->escape(false !== $choice_translation_domain ? $translatorHelper->trans($group_label, array(), $choice_translation_domain) : $group_label) ?>"> | ||
<?php echo $formHelper->block($form, 'choice_widget_options', array('choices' => $choice)) ?> | ||
</optgroup> | ||
<?php else: ?> | ||
<option value="<?php echo $view->escape($choice->value) ?>" <?php echo $view['form']->block($form, 'attributes', array('attr' => $choice->attr)) ?><?php if ($is_selected($choice->value, $value)): ?> selected="selected"<?php endif?>><?php echo $view->escape($translatorHelper->trans($choice->label, array(), $translation_domain)) ?></option> | ||
<option value="<?php echo $view->escape($choice->value) ?>" <?php echo $view['form']->block($form, 'attributes', array('attr' => $choice->attr)) ?><?php if ($is_selected($choice->value, $value)): ?> selected="selected"<?php endif?>><?php echo $view->escape(false !== $choice_translation_domain ? $translatorHelper->trans($choice->label, array(), $choice_translation_domain) : $choice->label) ?></option> | ||
<?php endif ?> | ||
<?php endforeach ?> |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -158,6 +158,11 @@ public function buildForm(FormBuilderInterface $builder, array $options) | |
*/ | ||
public function buildView(FormView $view, FormInterface $form, array $options) | ||
{ | ||
$choiceTranslationDomain = $options['choice_translation_domain']; | ||
if ($view->parent && null === $choiceTranslationDomain) { | ||
$choiceTranslationDomain = $view->vars['translation_domain']; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can use one if ($view->parent && null === $choiceTranslationDomain) {
// ...
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
|
||
/** @var ChoiceListView $choiceListView */ | ||
$choiceListView = $form->getConfig()->hasAttribute('choice_list_view') | ||
? $form->getConfig()->getAttribute('choice_list_view') | ||
|
@@ -170,6 +175,7 @@ public function buildView(FormView $view, FormInterface $form, array $options) | |
'choices' => $choiceListView->choices, | ||
'separator' => '-------------------', | ||
'placeholder' => null, | ||
'choice_translation_domain' => $choiceTranslationDomain, | ||
)); | ||
|
||
// The decision, whether a choice is selected, is potentially done | ||
|
@@ -295,6 +301,14 @@ public function configureOptions(OptionsResolver $resolver) | |
return $options['expanded']; | ||
}; | ||
|
||
$choiceTranslationDomainNormalizer = function (Options $options, $choiceTranslationDomain) { | ||
if (true === $choiceTranslationDomain) { | ||
return $options['translation_domain']; | ||
} | ||
|
||
return $choiceTranslationDomain; | ||
}; | ||
|
||
$resolver->setDefaults(array( | ||
'multiple' => false, | ||
'expanded' => false, | ||
|
@@ -317,14 +331,17 @@ public function configureOptions(OptionsResolver $resolver) | |
// is manually set to an object. | ||
// See https://github.com/symfony/symfony/pull/5582 | ||
'data_class' => null, | ||
'choice_translation_domain' => true, | ||
)); | ||
|
||
$resolver->setNormalizer('choice_list', $choiceListNormalizer); | ||
$resolver->setNormalizer('empty_value', $placeholderNormalizer); | ||
$resolver->setNormalizer('placeholder', $placeholderNormalizer); | ||
$resolver->setNormalizer('choice_translation_domain', $choiceTranslationDomainNormalizer); | ||
|
||
$resolver->setAllowedTypes('choice_list', array('null', 'Symfony\Component\Form\ChoiceList\ChoiceListInterface')); | ||
$resolver->setAllowedTypes('choices', array('null', 'array', '\Traversable')); | ||
$resolver->setAllowedTypes('choice_translation_domain', array('null', 'bool', 'string')); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this would also allow
<
F438
div hidden="hidden" data-view-component="true" class="js-comment-show-on-error flash flash-error flash-full">
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe the easiest would be to add a normalizer that return There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. or you set an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I used the normalizer. |
||
$resolver->setAllowedTypes('choices_as_values', 'bool'); | ||
$resolver->setAllowedTypes('choice_loader', array('null', 'Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface')); | ||
$resolver->setAllowedTypes('choice_label', array('null', 'callable', 'string', 'Symfony\Component\PropertyAccess\PropertyPath')); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just for the record .. this change breaks things for users of earlier Form component versions. maybe we should therefore adjust the composer dependency when we do such changes in the future