AdminType.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 Doctrine\Common\Collections\Collection;
  12. use Sonata\AdminBundle\Form\DataTransformer\ArrayToModelTransformer;
  13. use Symfony\Component\Form\AbstractType;
  14. use Symfony\Component\Form\FormBuilderInterface;
  15. use Symfony\Component\Form\FormInterface;
  16. use Symfony\Component\Form\FormView;
  17. use Symfony\Component\OptionsResolver\Options;
  18. use Symfony\Component\OptionsResolver\OptionsResolver;
  19. use Symfony\Component\OptionsResolver\OptionsResolverInterface;
  20. use Symfony\Component\PropertyAccess\Exception\NoSuchIndexException;
  21. use Symfony\Component\PropertyAccess\PropertyAccessor;
  22. /**
  23. * Class AdminType.
  24. *
  25. * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
  26. */
  27. class AdminType extends AbstractType
  28. {
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function buildForm(FormBuilderInterface $builder, array $options)
  33. {
  34. $admin = clone $this->getAdmin($options);
  35. if ($admin->hasParentFieldDescription()) {
  36. $admin->getParentFieldDescription()->setAssociationAdmin($admin);
  37. }
  38. if ($options['delete'] && $admin->isGranted('DELETE')) {
  39. if (!array_key_exists('translation_domain', $options['delete_options']['type_options'])) {
  40. $options['delete_options']['type_options']['translation_domain'] = $admin->getTranslationDomain();
  41. }
  42. $builder->add('_delete', $options['delete_options']['type'], $options['delete_options']['type_options']);
  43. }
  44. // hack to make sure the subject is correctly set
  45. // https://github.com/sonata-project/SonataAdminBundle/pull/2076
  46. if ($builder->getData() === null) {
  47. $p = new PropertyAccessor(false, true);
  48. try {
  49. $parentSubject = $admin->getParentFieldDescription()->getAdmin()->getSubject();
  50. if ($parentSubject !== null && $parentSubject !== false) {
  51. // for PropertyAccessor < 2.5
  52. // @todo remove this code for old PropertyAccessor after dropping support for Symfony 2.3
  53. if (!method_exists($p, 'isReadable')) {
  54. $subjectCollection = $p->getValue(
  55. $parentSubject,
  56. $this->getFieldDescription($options)->getFieldName()
  57. );
  58. if ($subjectCollection instanceof Collection) {
  59. $subject = $subjectCollection->get(trim($options['property_path'], '[]'));
  60. }
  61. } else {
  62. // for PropertyAccessor >= 2.5
  63. $subject = $p->getValue(
  64. $parentSubject,
  65. $this->getFieldDescription($options)->getFieldName().$options['property_path']
  66. );
  67. }
  68. $builder->setData($subject);
  69. }
  70. } catch (NoSuchIndexException $e) {
  71. // no object here
  72. }
  73. }
  74. $admin->setSubject($builder->getData());
  75. $admin->defineFormBuilder($builder);
  76. $builder->addModelTransformer(new ArrayToModelTransformer($admin->getModelManager(), $admin->getClass()));
  77. }
  78. /**
  79. * {@inheritdoc}
  80. */
  81. public function buildView(FormView $view, FormInterface $form, array $options)
  82. {
  83. $view->vars['btn_add'] = $options['btn_add'];
  84. $view->vars['btn_list'] = $options['btn_list'];
  85. $view->vars['btn_delete'] = $options['btn_delete'];
  86. $view->vars['btn_catalogue'] = $options['btn_catalogue'];
  87. }
  88. /**
  89. * {@inheritdoc}
  90. *
  91. * @todo Remove it when bumping requirements to SF 2.7+
  92. */
  93. public function setDefaultOptions(OptionsResolverInterface $resolver)
  94. {
  95. $this->configureOptions($resolver);
  96. }
  97. /**
  98. * {@inheritdoc}
  99. */
  100. public function configureOptions(OptionsResolver $resolver)
  101. {
  102. $resolver->setDefaults(array(
  103. 'delete' => function (Options $options) {
  104. return ($options['btn_delete'] !== false);
  105. },
  106. 'delete_options' => array(
  107. 'type' => 'checkbox',
  108. 'type_options' => array(
  109. 'required' => false,
  110. 'mapped' => false,
  111. ),
  112. ),
  113. 'auto_initialize' => false,
  114. 'btn_add' => 'link_add',
  115. 'btn_list' => 'link_list',
  116. 'btn_delete' => 'link_delete',
  117. 'btn_catalogue' => 'SonataAdminBundle',
  118. ));
  119. }
  120. /**
  121. * @param array $options
  122. *
  123. * @return \Sonata\AdminBundle\Admin\FieldDescriptionInterface
  124. *
  125. * @throws \RuntimeException
  126. */
  127. protected function getFieldDescription(array $options)
  128. {
  129. if (!isset($options['sonata_field_description'])) {
  130. throw new \RuntimeException('Please provide a valid `sonata_field_description` option');
  131. }
  132. return $options['sonata_field_description'];
  133. }
  134. /**
  135. * @param array $options
  136. *
  137. * @return \Sonata\AdminBundle\Admin\AdminInterface
  138. */
  139. protected function getAdmin(array $options)
  140. {
  141. return $this->getFieldDescription($options)->getAssociationAdmin();
  142. }
  143. /**
  144. * {@inheritdoc}
  145. */
  146. public function getName()
  147. {
  148. return 'sonata_type_admin';
  149. }
  150. }