FormMapper.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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;
  12. use Sonata\AdminBundle\Builder\FormContractorInterface;
  13. use Sonata\AdminBundle\Admin\AdminInterface;
  14. use Sonata\AdminBundle\Mapper\BaseGroupedMapper;
  15. use Symfony\Component\Form\FormBuilderInterface;
  16. /**
  17. * This class is use to simulate the Form API
  18. *
  19. */
  20. class FormMapper extends BaseGroupedMapper
  21. {
  22. /**
  23. * @var FormBuilderInterface
  24. */
  25. protected $formBuilder;
  26. /**
  27. * @param FormContractorInterface $formContractor
  28. * @param FormBuilderInterface $formBuilder
  29. * @param AdminInterface $admin
  30. */
  31. public function __construct(FormContractorInterface $formContractor, FormBuilderInterface $formBuilder, AdminInterface $admin)
  32. {
  33. parent::__construct($formContractor, $admin);
  34. $this->formBuilder = $formBuilder;
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function reorder(array $keys)
  40. {
  41. $this->admin->reorderFormGroup($this->getCurrentGroupName(), $keys);
  42. return $this;
  43. }
  44. /**
  45. * @param string $name
  46. * @param string $type
  47. * @param array $options
  48. * @param array $fieldDescriptionOptions
  49. *
  50. * @return $this
  51. */
  52. public function add($name, $type = null, array $options = array(), array $fieldDescriptionOptions = array())
  53. {
  54. if ($this->apply !== null && !$this->apply) {
  55. return $this;
  56. }
  57. if ($name instanceof FormBuilderInterface) {
  58. $fieldName = $name->getName();
  59. } else {
  60. $fieldName = $name;
  61. }
  62. // "Dot" notation is not allowed as form name, but can be used as property path to access nested data.
  63. if (!$name instanceof FormBuilderInterface && strpos($fieldName, '.')!==false && !isset($options['property_path'])) {
  64. $options['property_path'] = $fieldName;
  65. // fix the form name
  66. $fieldName = str_replace('.', '__', $fieldName);
  67. }
  68. // change `collection` to `sonata_type_native_collection` form type to
  69. // avoid BC break problems
  70. if ($type == 'collection') {
  71. $type = 'sonata_type_native_collection';
  72. }
  73. $label = $fieldName;
  74. $group = $this->addFieldToCurrentGroup($label);
  75. if (!isset($fieldDescriptionOptions['type']) && is_string($type)) {
  76. $fieldDescriptionOptions['type'] = $type;
  77. }
  78. if ($group['translation_domain'] && !isset($fieldDescriptionOptions['translation_domain'])) {
  79. $fieldDescriptionOptions['translation_domain'] = $group['translation_domain'];
  80. }
  81. $fieldDescription = $this->admin->getModelManager()->getNewFieldDescriptionInstance(
  82. $this->admin->getClass(),
  83. $name instanceof FormBuilderInterface ? $name->getName() : $name,
  84. $fieldDescriptionOptions
  85. );
  86. // Note that the builder var is actually the formContractor:
  87. $this->builder->fixFieldDescription($this->admin, $fieldDescription, $fieldDescriptionOptions);
  88. if ($fieldName != $name) {
  89. $fieldDescription->setName($fieldName);
  90. }
  91. $this->admin->addFormFieldDescription($fieldName, $fieldDescription);
  92. if ($name instanceof FormBuilderInterface) {
  93. $this->formBuilder->add($name);
  94. } else {
  95. // Note that the builder var is actually the formContractor:
  96. $options = array_replace_recursive($this->builder->getDefaultOptions($type, $fieldDescription), $options);
  97. // be compatible with mopa if not installed, avoid generating an exception for invalid option
  98. // force the default to false ...
  99. if (!isset($options['label_render'])) {
  100. $options['label_render'] = false;
  101. }
  102. if (!isset($options['label'])) {
  103. $options['label'] = $this->admin->getLabelTranslatorStrategy()->getLabel($fieldDescription->getName(), 'form', 'label');
  104. }
  105. $help = null;
  106. if (isset($options['help'])) {
  107. $help = $options['help'];
  108. unset($options['help']);
  109. }
  110. $this->formBuilder->add($fieldDescription->getName(), $type, $options);
  111. if (null !== $help) {
  112. $this->admin->getFormFieldDescription($fieldDescription->getName())->setHelp($help);
  113. }
  114. }
  115. return $this;
  116. }
  117. /**
  118. * {@inheritdoc}
  119. */
  120. public function get($name)
  121. {
  122. return $this->formBuilder->get($name);
  123. }
  124. /**
  125. * {@inheritdoc}
  126. */
  127. public function has($key)
  128. {
  129. return $this->formBuilder->has($key);
  130. }
  131. /**
  132. * {@inheritdoc}
  133. */
  134. public function remove($key)
  135. {
  136. $this->admin->removeFormFieldDescription($key);
  137. $this->admin->removeFieldFromFormGroup($key);
  138. $this->formBuilder->remove($key);
  139. return $this;
  140. }
  141. /**
  142. * @return \Symfony\Component\Form\FormBuilderInterface
  143. */
  144. public function getFormBuilder()
  145. {
  146. return $this->formBuilder;
  147. }
  148. /**
  149. * @param string $name
  150. * @param mixed $type
  151. * @param array $options
  152. *
  153. * @return \Symfony\Component\Form\FormBuilderInterface
  154. */
  155. public function create($name, $type = null, array $options = array())
  156. {
  157. return $this->formBuilder->create($name, $type, $options);
  158. }
  159. /**
  160. * @param array $helps
  161. *
  162. * @return FormMapper
  163. */
  164. public function setHelps(array $helps = array())
  165. {
  166. foreach ($helps as $name => $help) {
  167. if ($this->admin->hasFormFieldDescription($name)) {
  168. $this->admin->getFormFieldDescription($name)->setHelp($help);
  169. }
  170. }
  171. return $this;
  172. }
  173. /**
  174. * {@inheritdoc}
  175. */
  176. protected function getGroups()
  177. {
  178. return $this->admin->getFormGroups();
  179. }
  180. /**
  181. * {@inheritdoc}
  182. */
  183. protected function setGroups(array $groups)
  184. {
  185. $this->admin->setFormGroups($groups);
  186. }
  187. /**
  188. * {@inheritdoc}
  189. */
  190. protected function getTabs()
  191. {
  192. return $this->admin->getFormTabs();
  193. }
  194. /**
  195. * {@inheritdoc}
  196. */
  197. protected function setTabs(array $tabs)
  198. {
  199. $this->admin->setFormTabs($tabs);
  200. }
  201. }