ModelType.php 4.1 KB

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