FormMapper.php 6.2 KB

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