ModelReferenceType.php 1.7 KB

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