PageRenderTime 52ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/Form/Type/AdminType.php

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