AdminType.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. // for PropertyAccessor < 2.5
  50. // @todo remove this code for old PropertyAccessor after dropping support for Symfony 2.3
  51. if (!method_exists($p, 'isReadable')) {
  52. $subjectCollection = $p->getValue(
  53. $admin->getParentFieldDescription()->getAdmin()->getSubject(),
  54. $this->getFieldDescription($options)->getFieldName()
  55. );
  56. if ($subjectCollection instanceof Collection) {
  57. $subject = $subjectCollection->get(trim($options['property_path'], '[]'));
  58. }
  59. } else {
  60. // for PropertyAccessor >= 2.5
  61. $subject = $p->getValue(
  62. $admin->getParentFieldDescription()->getAdmin()->getSubject(),
  63. $this->getFieldDescription($options)->getFieldName().$options['property_path']
  64. );
  65. }
  66. $builder->setData($subject);
  67. } catch (NoSuchIndexException $e) {
  68. // no object here
  69. }
  70. }
  71. $admin->setSubject($builder->getData());
  72. $admin->defineFormBuilder($builder);
  73. $builder->addModelTransformer(new ArrayToModelTransformer($admin->getModelManager(), $admin->getClass()));
  74. }
  75. /**
  76. * {@inheritdoc}
  77. */
  78. public function buildView(FormView $view, FormInterface $form, array $options)
  79. {
  80. $view->vars['btn_add'] = $options['btn_add'];
  81. $view->vars['btn_list'] = $options['btn_list'];
  82. $view->vars['btn_delete'] = $options['btn_delete'];
  83. $view->vars['btn_catalogue'] = $options['btn_catalogue'];
  84. }
  85. /**
  86. * {@inheritdoc}
  87. *
  88. * @todo Remove it when bumping requirements to SF 2.7+
  89. */
  90. public function setDefaultOptions(OptionsResolverInterface $resolver)
  91. {
  92. $this->configureOptions($resolver);
  93. }
  94. /**
  95. * {@inheritDoc}
  96. */
  97. public function configureOptions(OptionsResolver $resolver)
  98. {
  99. $resolver->setDefaults(array(
  100. 'delete' => function (Options $options) {
  101. return ($options['btn_delete'] !== false);
  102. },
  103. 'delete_options' => array(
  104. 'type' => 'checkbox',
  105. 'type_options' => array(
  106. 'required' => false,
  107. 'mapped' => false,
  108. ),
  109. ),
  110. 'auto_initialize' => false,
  111. 'btn_add' => 'link_add',
  112. 'btn_list' => 'link_list',
  113. 'btn_delete' => 'link_delete',
  114. 'btn_catalogue' => 'SonataAdminBundle',
  115. ));
  116. }
  117. /**
  118. * @param array $options
  119. *
  120. * @return \Sonata\AdminBundle\Admin\FieldDescriptionInterface
  121. *
  122. * @throws \RuntimeException
  123. */
  124. protected function getFieldDescription(array $options)
  125. {
  126. if (!isset($options['sonata_field_description'])) {
  127. throw new \RuntimeException('Please provide a valid `sonata_field_description` option');
  128. }
  129. return $options['sonata_field_description'];
  130. }
  131. /**
  132. * @param array $options
  133. *
  134. * @return \Sonata\AdminBundle\Admin\AdminInterface
  135. */
  136. protected function getAdmin(array $options)
  137. {
  138. return $this->getFieldDescription($options)->getAssociationAdmin();
  139. }
  140. /**
  141. * {@inheritDoc}
  142. */
  143. public function getName()
  144. {
  145. return 'sonata_type_admin';
  146. }
  147. }