ModelType.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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\OptionsResolverInterface;
  22. /**
  23. * This type define a standard select input with a + sign to add new associated object.
  24. */
  25. class ModelType extends AbstractType
  26. {
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public function buildForm(FormBuilderInterface $builder, array $options)
  31. {
  32. if ($options['multiple']) {
  33. $builder
  34. ->addEventSubscriber(new MergeCollectionListener($options['model_manager']))
  35. ->addViewTransformer(new ModelsToArrayTransformer($options['choice_list']), true);
  36. } else {
  37. $builder
  38. ->addViewTransformer(new ModelToIdTransformer($options['model_manager'], $options['class']), true)
  39. ;
  40. }
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function buildView(FormView $view, FormInterface $form, array $options)
  46. {
  47. $view->vars['btn_add'] = $options['btn_add'];
  48. $view->vars['btn_list'] = $options['btn_list'];
  49. $view->vars['btn_delete'] = $options['btn_delete'];
  50. $view->vars['btn_catalogue'] = $options['btn_catalogue'];
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function setDefaultOptions(OptionsResolverInterface $resolver)
  56. {
  57. $resolver->setDefaults(array(
  58. 'compound' => function (Options $options) {
  59. if (isset($options['multiple']) && $options['multiple']) {
  60. if (isset($options['expanded']) && $options['expanded']) {
  61. //checkboxes
  62. return true;
  63. }
  64. //select tag (with multiple attribute)
  65. return false;
  66. }
  67. if (isset($options['expanded']) && $options['expanded']) {
  68. //radio buttons
  69. return true;
  70. }
  71. //select tag
  72. return false;
  73. },
  74. 'template' => 'choice',
  75. 'multiple' => false,
  76. 'expanded' => false,
  77. 'model_manager' => null,
  78. 'class' => null,
  79. 'property' => null,
  80. 'query' => null,
  81. 'choices' => null,
  82. 'preferred_choices' => array(),
  83. 'btn_add' => 'link_add',
  84. 'btn_list' => 'link_list',
  85. 'btn_delete' => 'link_delete',
  86. 'btn_catalogue' => 'SonataAdminBundle',
  87. 'choice_list' => function (Options $options, $previousValue) {
  88. if ($previousValue instanceof ChoiceListInterface && count($choices = $previousValue->getChoices())) {
  89. return $choices;
  90. }
  91. return new ModelChoiceList(
  92. $options['model_manager'],
  93. $options['class'],
  94. $options['property'],
  95. $options['query'],
  96. $options['choices']
  97. );
  98. },
  99. ));
  100. }
  101. /**
  102. * {@inheritdoc}
  103. */
  104. public function getParent()
  105. {
  106. return 'choice';
  107. }
  108. /**
  109. * {@inheritdoc}
  110. */
  111. public function getName()
  112. {
  113. return 'sonata_type_model';
  114. }
  115. }