AdminType.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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'] && $admin->isGranted('DELETE') ) {
  27. $builder->add('_delete', 'checkbox', array('required' => false, 'property_path' => false));
  28. }
  29. if (!$admin->hasSubject()) {
  30. $admin->setSubject($builder->getData());
  31. }
  32. $admin->defineFormBuilder($builder);
  33. $builder->prependClientTransformer(new ArrayToModelTransformer($admin->getModelManager(), $admin->getClass()));
  34. }
  35. /**
  36. * @param array $options
  37. * @return $options
  38. */
  39. public function getDefaultOptions(array $options)
  40. {
  41. return array(
  42. 'delete' => true,
  43. );
  44. }
  45. /**
  46. * @param array $options
  47. * @return \Sonata\AdminBundle\Admin\FieldDescriptionInterface
  48. */
  49. public function getFieldDescription(array $options)
  50. {
  51. if (!isset($options['sonata_field_description'])) {
  52. throw new \RuntimeException('Please provide a valid `sonata_field_description` option');
  53. }
  54. return $options['sonata_field_description'];
  55. }
  56. /**
  57. * @param array $options
  58. * @return \Sonata\AdminBundle\Admin\AdminInterface
  59. */
  60. public function getAdmin(array $options)
  61. {
  62. return $this->getFieldDescription($options)->getAssociationAdmin();
  63. }
  64. public function getName()
  65. {
  66. return 'sonata_type_admin';
  67. }
  68. }