ModelReferenceType.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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\FormBuilderInterface;
  13. use Symfony\Component\Form\FormFactoryInterface;
  14. use Symfony\Component\Form\AbstractType;
  15. use Symfony\Component\Form\FormInterface;
  16. use Symfony\Component\Form\FormView;
  17. use Symfony\Component\OptionsResolver\OptionsResolverInterface;
  18. use Sonata\AdminBundle\Form\EventListener\MergeCollectionListener;
  19. use Sonata\AdminBundle\Form\ChoiceList\ModelChoiceList;
  20. use Sonata\AdminBundle\Form\DataTransformer\ModelsToArrayTransformer;
  21. use Sonata\AdminBundle\Form\DataTransformer\ModelToIdTransformer;
  22. use Sonata\AdminBundle\Model\ModelManagerInterface;
  23. class ModelReferenceType extends AbstractType
  24. {
  25. /**
  26. * {@inheritDoc}
  27. */
  28. public function buildForm(FormBuilderInterface $builder, array $options)
  29. {
  30. $builder->prependClientTransformer(new ModelToIdTransformer($options['model_manager'], $options['class']));
  31. }
  32. public function createBuilder($name, FormFactoryInterface $factory, array $options)
  33. {
  34. return parent::createBuilder($name, $factory, $options);
  35. $this->parent = $options['parent'];
  36. }
  37. public function setDefaultOptions(OptionsResolverInterface $resolver)
  38. {
  39. parent::setDefaultOptions($resolver);
  40. $compound = function (Options $options) {
  41. return $options['parent'];
  42. };
  43. $resolver->setDefaults(array(
  44. 'compound' => $compound,
  45. ));
  46. }
  47. /**
  48. * {@inheritDoc}
  49. */
  50. public function getDefaultOptions()
  51. {
  52. return array(
  53. 'model_manager' => null,
  54. 'class' => null,
  55. 'parent' => 'hidden',
  56. );
  57. }
  58. /**
  59. * {@inheritDoc}
  60. */
  61. public function getParent()
  62. {
  63. return 'form';
  64. }
  65. /**
  66. * {@inheritDoc}
  67. */
  68. public function getName()
  69. {
  70. return 'sonata_type_model_reference';
  71. }
  72. }