FormMapper.php 6.5 KB

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