ModelType.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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\FormFactoryInterface;
  15. use Symfony\Component\Form\AbstractType;
  16. use Symfony\Component\Form\FormInterface;
  17. use Symfony\Component\Form\FormView;
  18. use Symfony\Component\OptionsResolver\Options;
  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. use Sonata\AdminBundle\Model\ModelManagerInterface;
  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. ->prependClientTransformer(new ModelsToArrayTransformer($options['choice_list']));
  36. } else {
  37. $builder->prependClientTransformer(new ModelToIdTransformer($options['model_manager'], $options['class']));
  38. }
  39. }
  40. public function createBuilder($name, FormFactoryInterface $factory, array $options)
  41. {
  42. return parent::createBuilder($name, $factory, $options);
  43. }
  44. public function setDefaultOptions(OptionsResolverInterface $resolver)
  45. {
  46. parent::setDefaultOptions($resolver);
  47. $compound = function (Options $options) {
  48. return isset($options['parent']) ? $options['parent'] : 'choice';
  49. };
  50. $resolver->setDefaults(array(
  51. 'compound' => $compound,
  52. ));
  53. }
  54. /**
  55. * {@inheritDoc}
  56. */
  57. public function getDefaultOptions()
  58. {
  59. $options = array(
  60. 'template' => 'choice',
  61. 'multiple' => false,
  62. 'expanded' => false,
  63. 'model_manager' => null,
  64. 'class' => null,
  65. 'property' => null,
  66. 'query' => null,
  67. 'choices' => null,
  68. 'parent' => 'choice',
  69. 'preferred_choices' => array(),
  70. 'choice_list' => function (Options $options, $previousValue) {
  71. if ($previousValue instanceof ChoiceListInterface
  72. && count($choices = $previousValue->getChoices())) {
  73. return $choices;
  74. }
  75. return new ModelChoiceList(
  76. $options['model_manager'],
  77. $options['class'],
  78. $options['property'],
  79. $options['query'],
  80. $options['choices']
  81. );
  82. }
  83. );
  84. return $options;
  85. }
  86. /**
  87. * {@inheritDoc}
  88. */
  89. public function getParent()
  90. {
  91. return 'choice';
  92. }
  93. /**
  94. * {@inheritDoc}
  95. */
  96. public function getName()
  97. {
  98. return 'sonata_type_model';
  99. }
  100. }