FormMapper.php 6.8 KB

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