AdminType.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. /*
  3. * This file is part of the Sonata 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. */
  11. namespace Sonata\AdminBundle\Form\Type;
  12. use Symfony\Component\Form\AbstractType;
  13. use Symfony\Component\Form\FormTypeInterface;
  14. use Symfony\Component\Form\FormBuilder;
  15. use Sonata\AdminBundle\Form\DataTransformer\ArrayToModelTransformer;
  16. class AdminType extends AbstractType
  17. {
  18. /**
  19. * @param \Symfony\Component\Form\FormBuilder $builder
  20. * @param array $options
  21. * @return void
  22. */
  23. public function buildForm(FormBuilder $builder, array $options)
  24. {
  25. $admin = $this->getAdmin($options);
  26. if ($options['delete']) {
  27. $builder->add('_delete', 'checkbox', array('required' => false, 'property_path' => false));
  28. }
  29. $admin->defineFormBuilder($builder);
  30. $builder->prependClientTransformer(new ArrayToModelTransformer($admin->getModelManager(), $admin->getClass()));
  31. }
  32. /**
  33. * @param array $options
  34. * @return $options
  35. */
  36. public function getDefaultOptions(array $options)
  37. {
  38. return array(
  39. 'field_description' => null,
  40. 'delete' => true,
  41. );
  42. }
  43. /**
  44. * @param array $options
  45. * @return \Sonata\AdminBundle\Admin\FieldDescriptionInterface
  46. */
  47. public function getFieldDescription(array $options)
  48. {
  49. return $options['field_description'];
  50. }
  51. /**
  52. * @param array $options
  53. * @return \Sonata\AdminBundle\Admin\AdminInterface
  54. */
  55. public function getAdmin(array $options)
  56. {
  57. return $this->getFieldDescription($options)->getAssociationAdmin();
  58. }
  59. public function getName()
  60. {
  61. return 'sonata_model_admin';
  62. }
  63. }