AdminType.php 3.6 KB

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