ChoiceTypeExtension.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. * Class ChoiceTypeExtension.
  18. *
  19. * @author Amine Zaghdoudi <amine.zaghdoudi@ekino.com>
  20. */
  21. class ChoiceTypeExtension extends AbstractTypeExtension
  22. {
  23. /**
  24. * {@inheritdoc}
  25. *
  26. * @todo Remove it when bumping requirements to SF 2.7+
  27. */
  28. public function setDefaultOptions(OptionsResolverInterface $resolver)
  29. {
  30. $this->configureOptions($resolver);
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function configureOptions(OptionsResolver $resolver)
  36. {
  37. $optionalOptions = array('sortable');
  38. if (method_exists($resolver, 'setDefined')) {
  39. $resolver->setDefined($optionalOptions);
  40. } else {
  41. // To keep Symfony <2.6 support
  42. $resolver->setOptional($optionalOptions);
  43. }
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function buildView(FormView $view, FormInterface $form, array $options)
  49. {
  50. $view->vars['sortable'] = array_key_exists('sortable', $options) && $options['sortable'];
  51. }
  52. /**
  53. * Returns the name of the type being extended.
  54. *
  55. * @return string The name of the type being extended
  56. */
  57. public function getExtendedType()
  58. {
  59. return 'choice';
  60. }
  61. }