AdminType.php 4.7 KB

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