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\AbstractType;
  15. use Symfony\Component\OptionsResolver\Options;
  16. use Symfony\Component\OptionsResolver\OptionsResolverInterface;
  17. use Sonata\AdminBundle\Form\EventListener\MergeCollectionListener;
  18. use Sonata\AdminBundle\Form\ChoiceList\ModelChoiceList;
  19. use Sonata\AdminBundle\Form\DataTransformer\ModelsToArrayTransformer;
  20. use Sonata\AdminBundle\Form\DataTransformer\ModelToIdTransformer;
  21. /**
  22. * This type define a standard select input with a + sign to add new associated object
  23. *
  24. */
  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. ->addViewTransformer(new ModelsToArrayTransformer($options['choice_list']), true);
  36. } else {
  37. $builder
  38. ->addViewTransformer(new ModelToIdTransformer($options['model_manager'], $options['class']), true)
  39. ;
  40. }
  41. }
  42. /**
  43. * {@inheritDoc}
  44. */
  45. public function setDefaultOptions(OptionsResolverInterface $resolver)
  46. {
  47. $resolver->setDefaults(array(
  48. 'compound' => function (Options $options) {
  49. return isset($options['multiple']) ? $options['multiple'] : false;
  50. },
  51. 'template' => 'choice',
  52. 'multiple' => false,
  53. 'expanded' => false,
  54. 'model_manager' => null,
  55. 'class' => null,
  56. 'property' => null,
  57. 'query' => null,
  58. 'choices' => null,
  59. 'preferred_choices' => array(),
  60. 'choice_list' => function (Options $options, $previousValue) {
  61. if ($previousValue instanceof ChoiceListInterface && count($choices = $previousValue->getChoices())) {
  62. return $choices;
  63. }
  64. return new ModelChoiceList(
  65. $options['model_manager'],
  66. $options['class'],
  67. $options['property'],
  68. $options['query'],
  69. $options['choices']
  70. );
  71. }
  72. ));
  73. }
  74. /**
  75. * {@inheritDoc}
  76. */
  77. public function getParent()
  78. {
  79. return 'choice';
  80. }
  81. /**
  82. * {@inheritDoc}
  83. */
  84. public function getName()
  85. {
  86. return 'sonata_type_model';
  87. }
  88. }