FormMapper.php 5.2 KB

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