ModelReferenceType.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. * NEXT_MAJOR: Remove method, when bumping requirements to SF 2.7+.
  32. *
  33. * {@inheritdoc}
  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. // NEXT_MAJOR: Remove ternary (when requirement of Symfony is >= 2.8)
  56. return method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix')
  57. ? 'Symfony\Component\Form\Extension\Core\Type\TextType'
  58. : 'text';
  59. }
  60. /**
  61. * NEXT_MAJOR: Remove when dropping Symfony <2.8 support.
  62. *
  63. * {@inheritdoc}
  64. */
  65. public function getName()
  66. {
  67. return $this->getBlockPrefix();
  68. }
  69. /**
  70. * {@inheritdoc}
  71. */
  72. public function getBlockPrefix()
  73. {
  74. return 'sonata_type_model_reference';
  75. }
  76. }