FormMapper.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. $label = $fieldName;
  65. $group = $this->addFieldToCurrentGroup($label);
  66. if (!isset($fieldDescriptionOptions['type']) && is_string($type)) {
  67. $fieldDescriptionOptions['type'] = $type;
  68. }
  69. if ($group['translation_domain'] && !isset($fieldDescriptionOptions['translation_domain'])) {
  70. $fieldDescriptionOptions['translation_domain'] = $group['translation_domain'];
  71. }
  72. $fieldDescription = $this->admin->getModelManager()->getNewFieldDescriptionInstance(
  73. $this->admin->getClass(),
  74. $name instanceof FormBuilder ? $name->getName() : $name,
  75. $fieldDescriptionOptions
  76. );
  77. // Note that the builder var is actually the formContractor:
  78. $this->builder->fixFieldDescription($this->admin, $fieldDescription, $fieldDescriptionOptions);
  79. if ($fieldName != $name) {
  80. $fieldDescription->setName($fieldName);
  81. }
  82. $this->admin->addFormFieldDescription($fieldName, $fieldDescription);
  83. if ($name instanceof FormBuilder) {
  84. $this->formBuilder->add($name);
  85. } else {
  86. // Note that the builder var is actually the formContractor:
  87. $options = array_replace_recursive($this->builder->getDefaultOptions($type, $fieldDescription), $options);
  88. if (!isset($options['label'])) {
  89. $options['label'] = $this->admin->getLabelTranslatorStrategy()->getLabel($fieldDescription->getName(), 'form', 'label');
  90. }
  91. $help = null;
  92. if (isset($options['help'])) {
  93. $help = $options['help'];
  94. unset($options['help']);
  95. }
  96. $this->formBuilder->add($fieldDescription->getName(), $type, $options);
  97. if (null !== $help) {
  98. $this->admin->getFormFieldDescription($fieldDescription->getName())->setHelp($help);
  99. }
  100. }
  101. return $this;
  102. }
  103. /**
  104. * @param string $name
  105. *
  106. * @return \Symfony\Component\Form\FormInterface
  107. */
  108. public function get($name)
  109. {
  110. return $this->formBuilder->get($name);
  111. }
  112. /**
  113. * @param string $key
  114. *
  115. * @return boolean
  116. */
  117. public function has($key)
  118. {
  119. return $this->formBuilder->has($key);
  120. }
  121. /**
  122. * @param string $key
  123. *
  124. * @return \Sonata\AdminBundle\Form\FormMapper
  125. */
  126. public function remove($key)
  127. {
  128. $this->admin->removeFormFieldDescription($key);
  129. $this->formBuilder->remove($key);
  130. return $this;
  131. }
  132. /**
  133. * @return \Symfony\Component\Form\FormBuilder
  134. */
  135. public function getFormBuilder()
  136. {
  137. return $this->formBuilder;
  138. }
  139. /**
  140. * @param string $name
  141. * @param mixed $type
  142. * @param array $options
  143. *
  144. * @return \Symfony\Component\Form\FormBuilder
  145. */
  146. public function create($name, $type = null, array $options = array())
  147. {
  148. return $this->formBuilder->create($name, $type, $options);
  149. }
  150. /**
  151. * @param array $helps
  152. *
  153. * @return FormMapper
  154. */
  155. public function setHelps(array $helps = array())
  156. {
  157. foreach ($helps as $name => $help) {
  158. if ($this->admin->hasFormFieldDescription($name)) {
  159. $this->admin->getFormFieldDescription($name)->setHelp($help);
  160. }
  161. }
  162. return $this;
  163. }
  164. /**
  165. * {@inheritdoc}
  166. */
  167. protected function getGroups()
  168. {
  169. return $this->admin->getFormGroups();
  170. }
  171. /**
  172. * {@inheritdoc}
  173. */
  174. protected function setGroups(array $groups)
  175. {
  176. $this->admin->setFormGroups($groups);
  177. }
  178. }