ModelType.php 2.9 KB

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