FormExtension.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  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 Symfony\Bridge\Twig\Extension;
  11. use Symfony\Bridge\Twig\TokenParser\FormThemeTokenParser;
  12. use Symfony\Component\Form\FormView;
  13. use Symfony\Component\Form\Exception\FormException;
  14. use Symfony\Component\Form\Util\FormUtil;
  15. /**
  16. * FormExtension extends Twig with form capabilities.
  17. *
  18. * @author Fabien Potencier <fabien@symfony.com>
  19. * @author Bernhard Schussek <bernhard.schussek@symfony.com>
  20. */
  21. class FormExtension extends \Twig_Extension
  22. {
  23. protected $resources;
  24. protected $blocks;
  25. protected $environment;
  26. protected $themes;
  27. protected $varStack;
  28. protected $template;
  29. public function __construct(array $resources = array())
  30. {
  31. $this->themes = new \SplObjectStorage();
  32. $this->varStack = new \SplObjectStorage();
  33. $this->blocks = new \SplObjectStorage();
  34. $this->resources = $resources;
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function initRuntime(\Twig_Environment $environment)
  40. {
  41. $this->environment = $environment;
  42. }
  43. /**
  44. * Sets a theme for a given view.
  45. *
  46. * @param FormView $view A FormView instance
  47. * @param array $resources An array of resources
  48. */
  49. public function setTheme(FormView $view, array $resources)
  50. {
  51. $this->themes->attach($view, $resources);
  52. $this->blocks = new \SplObjectStorage();
  53. }
  54. /**
  55. * Returns the token parser instance to add to the existing list.
  56. *
  57. * @return array An array of Twig_TokenParser instances
  58. */
  59. public function getTokenParsers()
  60. {
  61. return array(
  62. // {% form_theme form "SomeBungle::widgets.twig" %}
  63. new FormThemeTokenParser(),
  64. );
  65. }
  66. public function getFunctions()
  67. {
  68. return array(
  69. 'form_enctype' => new \Twig_Function_Method($this, 'renderEnctype', array('is_safe' => array('html'))),
  70. 'form_widget' => new \Twig_Function_Method($this, 'renderWidget', array('is_safe' => array('html'))),
  71. 'form_errors' => new \Twig_Function_Method($this, 'renderErrors', array('is_safe' => array('html'))),
  72. 'form_label' => new \Twig_Function_Method($this, 'renderLabel', array('is_safe' => array('html'))),
  73. 'form_row' => new \Twig_Function_Method($this, 'renderRow', array('is_safe' => array('html'))),
  74. 'form_rest' => new \Twig_Function_Method($this, 'renderRest', array('is_safe' => array('html'))),
  75. '_form_is_choice_group' => new \Twig_Function_Method($this, 'isChoiceGroup', array('is_safe' => array('html'))),
  76. '_form_is_choice_selected' => new \Twig_Function_Method($this, 'isChoiceSelected', array('is_safe' => array('html'))),
  77. );
  78. }
  79. public function isChoiceGroup($label)
  80. {
  81. return FormUtil::isChoiceGroup($label);
  82. }
  83. public function isChoiceSelected(FormView $view, $choice)
  84. {
  85. return FormUtil::isChoiceSelected($choice, $view->get('value'));
  86. }
  87. /**
  88. * Renders the HTML enctype in the form tag, if necessary
  89. *
  90. * Example usage in Twig templates:
  91. *
  92. * <form action="..." method="post" {{ form_enctype(form) }}>
  93. *
  94. * @param FormView $view The view for which to render the encoding type
  95. *
  96. * @return string The html markup
  97. */
  98. public function renderEnctype(FormView $view)
  99. {
  100. return $this->render($view, 'enctype');
  101. }
  102. /**
  103. * Renders a row for the view.
  104. *
  105. * @param FormView $view The view to render as a row
  106. * @param array $variables An array of variables
  107. *
  108. * @return string The html markup
  109. */
  110. public function renderRow(FormView $view, array $variables = array())
  111. {
  112. return $this->render($view, 'row', $variables);
  113. }
  114. /**
  115. * Renders views which have not already been rendered.
  116. *
  117. * @param FormView $view The parent view
  118. * @param array $variables An array of variables
  119. *
  120. * @return string The html markup
  121. */
  122. public function renderRest(FormView $view, array $variables = array())
  123. {
  124. return $this->render($view, 'rest', $variables);
  125. }
  126. /**
  127. * Renders the HTML for a given view
  128. *
  129. * Example usage in Twig:
  130. *
  131. * {{ form_widget(view) }}
  132. *
  133. * You can pass options during the call:
  134. *
  135. * {{ form_widget(view, {'attr': {'class': 'foo'}}) }}
  136. *
  137. * {{ form_widget(view, {'separator': '+++++'}) }}
  138. *
  139. * @param FormView $view The view to render
  140. * @param array $variables Additional variables passed to the template
  141. *
  142. * @return string The html markup
  143. */
  144. public function renderWidget(FormView $view, array $variables = array())
  145. {
  146. return $this->render($view, 'widget', $variables);
  147. }
  148. /**
  149. * Renders the errors of the given view
  150. *
  151. * @param FormView $view The view to render the errors for
  152. *
  153. * @return string The html markup
  154. */
  155. public function renderErrors(FormView $view)
  156. {
  157. return $this->render($view, 'errors');
  158. }
  159. /**
  160. * Renders the label of the given view
  161. *
  162. * @param FormView $view The view to render the label for
  163. * @param string $label Label name
  164. * @param array $variables Additional variables passed to the template
  165. *
  166. * @return string The html markup
  167. */
  168. public function renderLabel(FormView $view, $label = null, array $variables = array())
  169. {
  170. if ($label !== null) {
  171. $variables += array('label' => $label);
  172. }
  173. return $this->render($view, 'label', $variables);
  174. }
  175. /**
  176. * Renders a template.
  177. *
  178. * 1. This function first looks for a block named "_<view id>_<section>",
  179. * 2. if such a block is not found the function will look for a block named
  180. * "<type name>_<section>",
  181. * 3. the type name is recursively replaced by the parent type name until a
  182. * corresponding block is found
  183. *
  184. * @param FormView $view The form view
  185. * @param string $section The section to render (i.e. 'row', 'widget', 'label', ...)
  186. * @param array $variables Additional variables
  187. *
  188. * @return string The html markup
  189. *
  190. * @throws FormException if no template block exists to render the given section of the view
  191. */
  192. protected function render(FormView $view, $section, array $variables = array())
  193. {
  194. $mainTemplate = in_array($section, array('widget', 'row'));
  195. if ($mainTemplate && $view->isRendered()) {
  196. return '';
  197. }
  198. if (null === $this->template) {
  199. $this->template = reset($this->resources);
  200. if (!$this->template instanceof \Twig_Template) {
  201. $this->template = $this->environment->loadTemplate($this->template);
  202. }
  203. }
  204. $blocks = $this->getBlocks($view);
  205. $types = $view->get('types');
  206. array_unshift($types, '_'.$view->get('id'));
  207. foreach ($types as $type) {
  208. $block = $type.'_'.$section;
  209. if (isset($blocks[$block])) {
  210. $this->varStack[$view] = array_replace(
  211. $view->all(),
  212. isset($this->varStack[$view]) ? $this->varStack[$view] : array(),
  213. $variables
  214. );
  215. $html = $this->template->renderBlock($block, $this->varStack[$view], $blocks);
  216. if ($mainTemplate) {
  217. $view->setRendered();
  218. }
  219. unset($this->varStack[$view]);
  220. return $html;
  221. }
  222. }
  223. throw new FormException(sprintf('Unable to render form as none of the following blocks exist: "%s".', implode('", "', $blocks)));
  224. }
  225. /**
  226. * Returns the blocks used to render the view.
  227. *
  228. * Templates are looked for in the resources in the following order:
  229. * * resources from the themes (and its parents)
  230. * * resources from the themes of parent views (up to the root view)
  231. * * default resources
  232. *
  233. * @param FormView $view The view
  234. *
  235. * @return array An array of Twig_TemplateInterface instances
  236. */
  237. protected function getBlocks(FormView $view)
  238. {
  239. if (!$this->blocks->contains($view)) {
  240. $rootView = !$view->hasParent();
  241. $templates = $rootView ? $this->resources : array();
  242. if (isset($this->themes[$view])) {
  243. $templates = array_merge($templates, $this->themes[$view]);
  244. }
  245. $blocks = array();
  246. foreach ($templates as $template) {
  247. if (!$template instanceof \Twig_Template) {
  248. $template = $this->environment->loadTemplate($template);
  249. }
  250. $templateBlocks = array();
  251. do {
  252. $templateBlocks = array_merge($template->getBlocks(), $templateBlocks);
  253. } while (false !== $template = $template->getParent(array()));
  254. $blocks = array_merge($blocks, $templateBlocks);
  255. }
  256. if (!$rootView) {
  257. $blocks = array_merge($this->getBlocks($view->getParent()), $blocks);
  258. }
  259. $this->blocks->attach($view, $blocks);
  260. } else {
  261. $blocks = $this->blocks[$view];
  262. }
  263. return $blocks;
  264. }
  265. /**
  266. * Returns the name of the extension.
  267. *
  268. * @return string The extension name
  269. */
  270. public function getName()
  271. {
  272. return 'form';
  273. }
  274. }