ModelReferenceType.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. /*
  3. * This file is part of the Sonata Project 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. namespace Sonata\AdminBundle\Form\Type;
  11. use Sonata\AdminBundle\Form\DataTransformer\ModelToIdTransformer;
  12. use Symfony\Component\Form\AbstractType;
  13. use Symfony\Component\Form\FormBuilderInterface;
  14. use Symfony\Component\OptionsResolver\OptionsResolver;
  15. use Symfony\Component\OptionsResolver\OptionsResolverInterface;
  16. /**
  17. * Class ModelReferenceType.
  18. *
  19. * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
  20. */
  21. class ModelReferenceType extends AbstractType
  22. {
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public function buildForm(FormBuilderInterface $builder, array $options)
  27. {
  28. $builder->addModelTransformer(new ModelToIdTransformer($options['model_manager'], $options['class']));
  29. }
  30. /**
  31. * {@inheritdoc}
  32. *
  33. * @todo Remove it when bumping requirements to SF 2.7+
  34. */
  35. public function setDefaultOptions(OptionsResolverInterface $resolver)
  36. {
  37. $this->configureOptions($resolver);
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function configureOptions(OptionsResolver $resolver)
  43. {
  44. $resolver->setDefaults(array(
  45. 'compound' => false,
  46. 'model_manager' => null,
  47. 'class' => null,
  48. ));
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function getParent()
  54. {
  55. return 'text';
  56. }
  57. /**
  58. * {@inheritdoc}
  59. *
  60. * @todo Remove when dropping Symfony <2.8 support
  61. */
  62. public function getName()
  63. {
  64. return $this->getBlockPrefix();
  65. }
  66. /**
  67. * {@inheritdoc}
  68. */
  69. public function getBlockPrefix()
  70. {
  71. return 'sonata_type_model_reference';
  72. }
  73. }