123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- <?php
- /*
- * This file is part of the Sonata package.
- *
- * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- *
- */
- namespace Sonata\AdminBundle\Form\Type;
- use Symfony\Component\Translation\TranslatorInterface;
- use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
- use Symfony\Component\Form\FormInterface;
- use Symfony\Component\Form\FormView;
- use Symfony\Component\Form\FormBuilder;
- class TranslatableChoiceType extends ChoiceType
- {
- protected $translator;
- /**
- * @param \Symfony\Component\Translation\TranslatorInterface $translator
- */
- public function __construct(TranslatorInterface $translator)
- {
- $this->translator = $translator;
- }
- /**
- * @param array $options
- * @return array
- */
- public function getDefaultOptions(array $options)
- {
- $multiple = isset($options['multiple']) && $options['multiple'];
- $expanded = isset($options['expanded']) && $options['expanded'];
- return array(
- 'multiple' => false,
- 'expanded' => false,
- 'choice_list' => null,
- 'choices' => array(),
- 'preferred_choices' => array(),
- 'catalogue' => 'messages',
- 'empty_data' => $multiple || $expanded ? array() : '',
- 'empty_value' => $multiple || $expanded || !isset($options['empty_value']) ? null : '',
- 'error_bubbling' => false,
- );
- }
- public function buildForm(FormBuilder $builder, array $options)
- {
- // translate options before building form
- foreach ($options['choices'] as $name => $value) {
- $options['choices'][$name] = $this->translator->trans($value, array(), $options['catalogue']);
- }
-
- // translate empty value
- if (!empty($options['empty_value'])) {
- $options['empty_value'] = $this->translator->trans($options['empty_value'], array(), $options['catalogue']);
- }
-
- parent::buildForm($builder, $options);
- }
- }
|