ModelReferenceType.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 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 ModelReferenceType extends AbstractType
  25. {
  26. /**
  27. * {@inheritDoc}
  28. */
  29. public function buildForm(FormBuilderInterface $builder, array $options)
  30. {
  31. $builder->prependClientTransformer(new ModelToIdTransformer($options['model_manager'], $options['class']));
  32. }
  33. public function setDefaultOptions(OptionsResolverInterface $resolver)
  34. {
  35. $compound = function (Options $options) {
  36. return isset($options['parent']) ? $options['parent'] : 'hidden';
  37. };
  38. $resolver->setDefaults(array(
  39. 'compound' => $compound,
  40. 'model_manager' => null,
  41. 'class' => null
  42. ));
  43. }
  44. /**
  45. * {@inheritDoc}
  46. */
  47. public function getName()
  48. {
  49. return 'sonata_type_model_reference';
  50. }
  51. }