ModelHiddenType.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. * This type define a standard hidden field, that stored id to a object.
  18. *
  19. * @author Andrej Hudec <pulzarraider@gmail.com>
  20. */
  21. class ModelHiddenType extends AbstractType
  22. {
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public function buildForm(FormBuilderInterface $builder, array $options)
  27. {
  28. $builder
  29. ->addViewTransformer(new ModelToIdTransformer($options['model_manager'], $options['class']), true)
  30. ;
  31. }
  32. /**
  33. * {@inheritdoc}
  34. *
  35. * @todo Remove it when bumping requirements to SF 2.7+
  36. */
  37. public function setDefaultOptions(OptionsResolverInterface $resolver)
  38. {
  39. $this->configureOptions($resolver);
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function configureOptions(OptionsResolver $resolver)
  45. {
  46. $resolver->setDefaults(array(
  47. 'model_manager' => null,
  48. 'class' => null,
  49. ));
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. public function getParent()
  55. {
  56. return 'hidden';
  57. }
  58. /**
  59. * {@inheritdoc}
  60. *
  61. * @todo Remove when dropping Symfony <2.8 support
  62. */
  63. public function getName()
  64. {
  65. return $this->getBlockPrefix();
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. public function getBlockPrefix()
  71. {
  72. return 'sonata_type_model_hidden';
  73. }
  74. }