TranslatableChoiceType.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /*
  3. * This file is part of the Sonata package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. *
  10. */
  11. namespace Sonata\AdminBundle\Form\Type;
  12. use Symfony\Component\Translation\TranslatorInterface;
  13. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  14. use Symfony\Component\Form\FormInterface;
  15. use Symfony\Component\Form\FormView;
  16. use Symfony\Component\Form\FormBuilderInterface;
  17. use Symfony\Component\OptionsResolver\Options;
  18. use Symfony\Component\OptionsResolver\OptionsResolverInterface;
  19. class TranslatableChoiceType extends ChoiceType
  20. {
  21. protected $translator;
  22. /**
  23. * @param \Symfony\Component\Translation\TranslatorInterface $translator
  24. */
  25. public function __construct(TranslatorInterface $translator)
  26. {
  27. $this->translator = $translator;
  28. }
  29. /**
  30. * {@inheritedDoc}
  31. */
  32. public function setDefaultOptions(OptionsResolverInterface $resolver)
  33. {
  34. $resolver->setDefaults(array(
  35. 'multiple' => false,
  36. 'expanded' => false,
  37. 'choice_list' => null,
  38. 'choices' => array(),
  39. 'preferred_choices' => array(),
  40. 'catalogue' => 'messages',
  41. 'empty_data' => function (Options $options, $previousValue) {
  42. $multiple = isset($options['multiple']) && $options['multiple'];
  43. $expanded = isset($options['expanded']) && $options['expanded'];
  44. return $multiple || $expanded ? array() : '';
  45. },
  46. 'empty_value' => function (Options $options, $previousValue) {
  47. $multiple = isset($options['multiple']) && $options['multiple'];
  48. $expanded = isset($options['expanded']) && $options['expanded'];
  49. return $multiple || $expanded || !isset($options['empty_value']) ? null : '';
  50. },
  51. 'error_bubbling' => false,
  52. ));
  53. }
  54. /**
  55. * {@inheritDoc}
  56. */
  57. public function buildForm(FormBuilderInterface $builder, array $options)
  58. {
  59. // translate options before building form
  60. foreach ($options['choices'] as $name => $value) {
  61. $options['choices'][$name] = $this->translator->trans($value, array(), $options['catalogue']);
  62. }
  63. // translate empty value
  64. if (!empty($options['empty_value'])) {
  65. $options['empty_value'] = $this->translator->trans($options['empty_value'], array(), $options['catalogue']);
  66. }
  67. parent::buildForm($builder, $options);
  68. }
  69. /**
  70. * {@inheritDoc}
  71. */
  72. public function getParent()
  73. {
  74. return 'choice';
  75. }
  76. /**
  77. * {@inheritDoc}
  78. */
  79. public function getName()
  80. {
  81. return 'sonata_type_translatable_choice';
  82. }
  83. }