ModelType.php 3.0 KB

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