ModelHiddenType.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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\OptionsResolverInterface;
  15. /**
  16. * This type define a standard hidden field, that stored id to a object.
  17. *
  18. * @author Andrej Hudec <pulzarraider@gmail.com>
  19. */
  20. class ModelHiddenType extends AbstractType
  21. {
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public function buildForm(FormBuilderInterface $builder, array $options)
  26. {
  27. $builder
  28. ->addViewTransformer(new ModelToIdTransformer($options['model_manager'], $options['class']), true)
  29. ;
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function setDefaultOptions(OptionsResolverInterface $resolver)
  35. {
  36. $resolver->setDefaults(array(
  37. 'model_manager' => null,
  38. 'class' => null,
  39. ));
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function getParent()
  45. {
  46. return 'hidden';
  47. }
  48. /**
  49. * {@inheritdoc}
  50. */
  51. public function getName()
  52. {
  53. return 'sonata_type_model_hidden';
  54. }
  55. }