FormMapper.php 6.7 KB

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