AdminType.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. 'delete' => true,
  40. );
  41. }
  42. /**
  43. * @param array $options
  44. * @return \Sonata\AdminBundle\Admin\FieldDescriptionInterface
  45. */
  46. public function getFieldDescription(array $options)
  47. {
  48. if (!isset($options['sonata_field_description'])) {
  49. throw new \RuntimeException('Please provide a valid `sonata_field_description` option');
  50. }
  51. return $options['sonata_field_description'];
  52. }
  53. /**
  54. * @param array $options
  55. * @return \Sonata\AdminBundle\Admin\AdminInterface
  56. */
  57. public function getAdmin(array $options)
  58. {
  59. return $this->getFieldDescription($options)->getAssociationAdmin();
  60. }
  61. public function getName()
  62. {
  63. return 'sonata_type_admin';
  64. }
  65. }