AdminType.php 3.5 KB

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