ModelType.php 4.4 KB

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