FormMapper.php 6.3 KB

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