ChoiceFieldMaskType.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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\Type;
  11. use Symfony\Component\Form\AbstractType;
  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 ChoiceFieldMaskType.
  18. *
  19. * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
  20. */
  21. class ChoiceFieldMaskType extends AbstractType
  22. {
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public function buildView(FormView $view, FormInterface $form, array $options)
  27. {
  28. $allFieldNames = array();
  29. foreach ($options['map'] as $value => $fieldNames) {
  30. foreach ($fieldNames as $fieldName) {
  31. $allFieldNames[$fieldName] = $fieldName;
  32. }
  33. }
  34. $allFieldNames = array_values($allFieldNames);
  35. $view->vars['all_fields'] = $allFieldNames;
  36. $view->vars['map'] = $options['map'];
  37. $options['expanded'] = false;
  38. parent::buildView($view, $form, $options);
  39. }
  40. /**
  41. * {@inheritdoc}
  42. *
  43. * @todo Remove it when bumping requirements to SF 2.7+
  44. */
  45. public function setDefaultOptions(OptionsResolverInterface $resolver)
  46. {
  47. $this->configureOptions($resolver);
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function configureOptions(OptionsResolver $resolver)
  53. {
  54. // TODO: Remove conditional parent call when bumping requirements to SF 2.7+
  55. if (method_exists('Symfony\Component\Form\AbstractType', 'configureOptions')) {
  56. parent::configureOptions($resolver);
  57. } else {
  58. parent::setDefaultOptions($resolver);
  59. }
  60. $resolver->setDefaults(array(
  61. 'map' => array(),
  62. ));
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. public function getParent()
  68. {
  69. return 'choice';
  70. }
  71. /**
  72. * {@inheritdoc}
  73. *
  74. * @todo Remove when dropping Symfony <2.8 support
  75. */
  76. public function getName()
  77. {
  78. return $this->getBlockPrefix();
  79. }
  80. /**
  81. * {@inheritdoc}
  82. */
  83. public function getBlockPrefix()
  84. {
  85. return 'sonata_type_choice_field_mask';
  86. }
  87. }