ModelHiddenType.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. public function getName()
  62. {
  63. return 'sonata_type_model_hidden';
  64. }
  65. }