ModelType.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 setDefaultOptions(OptionsResolverInterface $resolver)
  41. {
  42. $compound = function (Options $options) {
  43. return isset($options['parent']) ? $options['parent'] : 'choice';
  44. };
  45. $resolver->setDefaults(array(
  46. 'compound' => $compound,
  47. ));
  48. }
  49. /**
  50. * {@inheritDoc}
  51. */
  52. public function getDefaultOptions()
  53. {
  54. $options = array(
  55. 'template' => 'choice',
  56. 'multiple' => false,
  57. 'expanded' => false,
  58. 'model_manager' => null,
  59. 'class' => null,
  60. 'property' => null,
  61. 'query' => null,
  62. 'choices' => null,
  63. 'parent' => 'choice',
  64. 'preferred_choices' => array(),
  65. 'choice_list' => function (Options $options, $previousValue) {
  66. if ($previousValue instanceof ChoiceListInterface
  67. && count($choices = $previousValue->getChoices())) {
  68. return $choices;
  69. }
  70. return new ModelChoiceList(
  71. $options['model_manager'],
  72. $options['class'],
  73. $options['property'],
  74. $options['query'],
  75. $options['choices']
  76. );
  77. }
  78. );
  79. return $options;
  80. }
  81. /**
  82. * {@inheritDoc}
  83. */
  84. public function getParent()
  85. {
  86. return 'choice';
  87. }
  88. /**
  89. * {@inheritDoc}
  90. */
  91. public function getName()
  92. {
  93. return 'sonata_type_model';
  94. }
  95. }