ModelReferenceType.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 setDefaultOptions(OptionsResolverInterface $resolver)
  33. {
  34. $compound = function (Options $options) {
  35. return $options['parent'];
  36. };
  37. $resolver->setDefaults(array(
  38. 'compound' => $compound,
  39. ));
  40. }
  41. /**
  42. * {@inheritDoc}
  43. */
  44. public function getDefaultOptions()
  45. {
  46. return array(
  47. 'model_manager' => null,
  48. 'class' => null,
  49. 'parent' => 'hidden',
  50. );
  51. }
  52. /**
  53. * {@inheritDoc}
  54. */
  55. public function getName()
  56. {
  57. return 'sonata_type_model_reference';
  58. }
  59. }