AdminType.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 = clone $this->getAdmin($options);
  27. if ($admin->hasParentFieldDescription()) {
  28. $admin->getParentFieldDescription()->setAssociationAdmin($admin);
  29. }
  30. if ($options['delete'] && $admin->isGranted('DELETE')) {
  31. if (!array_key_exists('translation_domain', $options['delete_options']['type_options'])) {
  32. $options['delete_options']['type_options']['translation_domain'] = $admin->getTranslationDomain();
  33. }
  34. $builder->add('_delete', $options['delete_options']['type'], $options['delete_options']['type_options']);
  35. }
  36. $admin->setSubject($builder->getData());
  37. $admin->defineFormBuilder($builder);
  38. $builder->addModelTransformer(new ArrayToModelTransformer($admin->getModelManager(), $admin->getClass()));
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function buildView(FormView $view, FormInterface $form, array $options)
  44. {
  45. $view->vars['btn_add'] = $options['btn_add'];
  46. $view->vars['btn_list'] = $options['btn_list'];
  47. $view->vars['btn_delete'] = $options['btn_delete'];
  48. $view->vars['btn_catalogue'] = $options['btn_catalogue'];
  49. }
  50. /**
  51. * {@inheritDoc}
  52. */
  53. public function setDefaultOptions(OptionsResolverInterface $resolver)
  54. {
  55. $resolver->setDefaults(array(
  56. 'delete' => function (Options $options) {
  57. return ($options['btn_delete'] !== false);
  58. },
  59. 'delete_options' => array(
  60. 'type' => 'checkbox',
  61. 'type_options' => array(
  62. 'required' => false,
  63. 'mapped' => false,
  64. ),
  65. ),
  66. 'auto_initialize' => false,
  67. 'btn_add' => 'link_add',
  68. 'btn_list' => 'link_list',
  69. 'btn_delete' => 'link_delete',
  70. 'btn_catalogue' => 'SonataAdminBundle'
  71. ));
  72. }
  73. /**
  74. * @param array $options
  75. *
  76. * @return \Sonata\AdminBundle\Admin\FieldDescriptionInterface
  77. *
  78. * @throws \RuntimeException
  79. */
  80. protected function getFieldDescription(array $options)
  81. {
  82. if (!isset($options['sonata_field_description'])) {
  83. throw new \RuntimeException('Please provide a valid `sonata_field_description` option');
  84. }
  85. return $options['sonata_field_description'];
  86. }
  87. /**
  88. * @param array $options
  89. *
  90. * @return \Sonata\AdminBundle\Admin\AdminInterface
  91. */
  92. protected function getAdmin(array $options)
  93. {
  94. return $this->getFieldDescription($options)->getAssociationAdmin();
  95. }
  96. /**
  97. * {@inheritDoc}
  98. */
  99. public function getName()
  100. {
  101. return 'sonata_type_admin';
  102. }
  103. }