ModelType.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 Sonata\AdminBundle\Form\ChoiceList\ModelChoiceList;
  12. use Sonata\AdminBundle\Form\DataTransformer\ModelsToArrayTransformer;
  13. use Sonata\AdminBundle\Form\DataTransformer\ModelToIdTransformer;
  14. use Sonata\AdminBundle\Form\EventListener\MergeCollectionListener;
  15. use Symfony\Component\Form\AbstractType;
  16. use Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface;
  17. use Symfony\Component\Form\FormBuilderInterface;
  18. use Symfony\Component\Form\FormInterface;
  19. use Symfony\Component\Form\FormView;
  20. use Symfony\Component\OptionsResolver\Options;
  21. use Symfony\Component\OptionsResolver\OptionsResolver;
  22. use Symfony\Component\OptionsResolver\OptionsResolverInterface;
  23. /**
  24. * Class ModelType
  25. * This type define a standard select input with a + sign to add new associated object.
  26. *
  27. * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
  28. */
  29. class ModelType extends AbstractType
  30. {
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function buildForm(FormBuilderInterface $builder, array $options)
  35. {
  36. if ($options['multiple']) {
  37. $builder
  38. ->addEventSubscriber(new MergeCollectionListener($options['model_manager']))
  39. ->addViewTransformer(new ModelsToArrayTransformer($options['choice_list']), true);
  40. } else {
  41. $builder
  42. ->addViewTransformer(new ModelToIdTransformer($options['model_manager'], $options['class']), true)
  43. ;
  44. }
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public function buildView(FormView $view, FormInterface $form, array $options)
  50. {
  51. $view->vars['btn_add'] = $options['btn_add'];
  52. $view->vars['btn_list'] = $options['btn_list'];
  53. $view->vars['btn_delete'] = $options['btn_delete'];
  54. $view->vars['btn_catalogue'] = $options['btn_catalogue'];
  55. }
  56. /**
  57. * {@inheritdoc}
  58. *
  59. * @todo Remove it when bumping requirements to SF 2.7+
  60. */
  61. public function setDefaultOptions(OptionsResolverInterface $resolver)
  62. {
  63. $this->configureOptions($resolver);
  64. }
  65. /**
  66. * {@inheritdoc}
  67. */
  68. public function configureOptions(OptionsResolver $resolver)
  69. {
  70. $resolver->setDefaults(array(
  71. 'compound' => function (Options $options) {
  72. if (isset($options['multiple']) && $options['multiple']) {
  73. if (isset($options['expanded']) && $options['expanded']) {
  74. //checkboxes
  75. return true;
  76. }
  77. //select tag (with multiple attribute)
  78. return false;
  79. }
  80. if (isset($options['expanded']) && $options['expanded']) {
  81. //radio buttons
  82. return true;
  83. }
  84. //select tag
  85. return false;
  86. },
  87. 'template' => 'choice',
  88. 'multiple' => false,
  89. 'expanded' => false,
  90. 'model_manager' => null,
  91. 'class' => null,
  92. 'property' => null,
  93. 'query' => null,
  94. 'choices' => null,
  95. 'preferred_choices' => array(),
  96. 'btn_add' => 'link_add',
  97. 'btn_list' => 'link_list',
  98. 'btn_delete' => 'link_delete',
  99. 'btn_catalogue' => 'SonataAdminBundle',
  100. 'choice_list' => function (Options $options, $previousValue) {
  101. if ($previousValue instanceof ChoiceListInterface && count($choices = $previousValue->getChoices())) {
  102. return $choices;
  103. }
  104. return new ModelChoiceList(
  105. $options['model_manager'],
  106. $options['class'],
  107. $options['property'],
  108. $options['query'],
  109. $options['choices']
  110. );
  111. },
  112. ));
  113. }
  114. /**
  115. * {@inheritdoc}
  116. */
  117. public function getParent()
  118. {
  119. return 'choice';
  120. }
  121. /**
  122. * {@inheritdoc}
  123. */
  124. public function getName()
  125. {
  126. return 'sonata_type_model';
  127. }
  128. }