ChoiceTypeExtension.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. /*
  3. * This file is part of the Sonata Project 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. namespace Sonata\AdminBundle\Form\Extension;
  11. use Symfony\Component\Form\AbstractTypeExtension;
  12. use Symfony\Component\Form\FormInterface;
  13. use Symfony\Component\Form\FormView;
  14. use Symfony\Component\OptionsResolver\OptionsResolver;
  15. use Symfony\Component\OptionsResolver\OptionsResolverInterface;
  16. /**
  17. * @author Amine Zaghdoudi <amine.zaghdoudi@ekino.com>
  18. */
  19. class ChoiceTypeExtension extends AbstractTypeExtension
  20. {
  21. /**
  22. * NEXT_MAJOR: Remove method, when bumping requirements to SF 2.7+.
  23. *
  24. * {@inheritdoc}
  25. */
  26. public function setDefaultOptions(OptionsResolverInterface $resolver)
  27. {
  28. $this->configureOptions($resolver);
  29. }
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public function configureOptions(OptionsResolver $resolver)
  34. {
  35. $optionalOptions = array('sortable');
  36. if (method_exists($resolver, 'setDefined')) {
  37. $resolver->setDefined($optionalOptions);
  38. } else {
  39. // To keep Symfony <2.6 support
  40. $resolver->setOptional($optionalOptions);
  41. }
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function buildView(FormView $view, FormInterface $form, array $options)
  47. {
  48. $view->vars['sortable'] = array_key_exists('sortable', $options) && $options['sortable'];
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function getExtendedType()
  54. {
  55. /*
  56. * NEXT_MAJOR: Remove when dropping Symfony <2.8 support. It should
  57. * simply be return 'Symfony\Component\Form\Extension\Core\Type\ChoiceType';
  58. */
  59. return method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix')
  60. ? 'Symfony\Component\Form\Extension\Core\Type\ChoiceType'
  61. : 'choice';
  62. }
  63. }