AdminType.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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\FormBuilderInterface;
  14. use Symfony\Component\Form\FormInterface;
  15. use Symfony\Component\Form\FormView;
  16. use Symfony\Component\OptionsResolver\Options;
  17. use Symfony\Component\OptionsResolver\OptionsResolverInterface;
  18. use Sonata\AdminBundle\Form\DataTransformer\ArrayToModelTransformer;
  19. class AdminType extends AbstractType
  20. {
  21. /**
  22. * {@inheritDoc}
  23. */
  24. public function buildForm(FormBuilderInterface $builder, array $options)
  25. {
  26. $admin = $this->getAdmin($options);
  27. if ($options['delete'] && $admin->isGranted('DELETE')) {
  28. $builder->add('_delete', 'checkbox', array('required' => false, 'mapped' => false, 'translation_domain' => $admin->getTranslationDomain()));
  29. }
  30. if (!$admin->hasSubject()) {
  31. $admin->setSubject($builder->getData());
  32. }
  33. $admin->defineFormBuilder($builder);
  34. $builder->addModelTransformer(new ArrayToModelTransformer($admin->getModelManager(), $admin->getClass()));
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function buildView(FormView $view, FormInterface $form, array $options)
  40. {
  41. $view->vars['btn_add'] = $options['btn_add'];
  42. $view->vars['btn_list'] = $options['btn_list'];
  43. $view->vars['btn_delete'] = $options['btn_delete'];
  44. $view->vars['btn_catalogue'] = $options['btn_catalogue'];
  45. }
  46. /**
  47. * {@inheritDoc}
  48. */
  49. public function setDefaultOptions(OptionsResolverInterface $resolver)
  50. {
  51. $resolver->setDefaults(array(
  52. 'delete' => function (Options $options) {
  53. return ($options['btn_delete'] !== false);
  54. },
  55. 'auto_initialize' => false,
  56. 'btn_add' => 'link_add',
  57. 'btn_list' => 'link_list',
  58. 'btn_delete' => 'link_delete',
  59. 'btn_catalogue' => 'SonataAdminBundle'
  60. ));
  61. }
  62. /**
  63. * @param array $options
  64. *
  65. * @return \Sonata\AdminBundle\Admin\FieldDescriptionInterface
  66. *
  67. * @throws \RuntimeException
  68. */
  69. protected function getFieldDescription(array $options)
  70. {
  71. if (!isset($options['sonata_field_description'])) {
  72. throw new \RuntimeException('Please provide a valid `sonata_field_description` option');
  73. }
  74. return $options['sonata_field_description'];
  75. }
  76. /**
  77. * @param array $options
  78. *
  79. * @return \Sonata\AdminBundle\Admin\AdminInterface
  80. */
  81. protected function getAdmin(array $options)
  82. {
  83. return $this->getFieldDescription($options)->getAssociationAdmin();
  84. }
  85. /**
  86. * {@inheritDoc}
  87. */
  88. public function getName()
  89. {
  90. return 'sonata_type_admin';
  91. }
  92. }