ModelReferenceType.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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\FormBuilder;
  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 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. use Sonata\AdminBundle\Model\ModelManagerInterface;
  22. class ModelReferenceType extends AbstractType
  23. {
  24. public function buildForm(FormBuilder $builder, array $options)
  25. {
  26. $builder->prependClientTransformer(new ModelToIdTransformer($options['model_manager'], $options['class']));
  27. }
  28. public function getDefaultOptions(array $options)
  29. {
  30. $defaultOptions = array(
  31. 'model_manager' => null,
  32. 'class' => null,
  33. 'parent' => 'hidden',
  34. );
  35. $options = array_replace($defaultOptions, $options);
  36. return $options;
  37. }
  38. public function getParent(array $options)
  39. {
  40. return $options['parent'];
  41. }
  42. public function getName()
  43. {
  44. return 'sonata_type_model_reference';
  45. }
  46. }