/
FormExtension.php
300 lines (263 loc) · 9.51 KB
/
FormExtension.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\TwigBundle\Extension;
use Symfony\Component\Form\Form;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FieldInterface;
use Symfony\Component\Form\CollectionField;
use Symfony\Component\Form\HybridField;
use Symfony\Bundle\TwigBundle\TokenParser\FormThemeTokenParser;
/**
* FormExtension extends Twig with form capabilities.
*
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
* @author Bernhard Schussek <bernhard.schussek@symfony-project.com>
*/
class FormExtension extends \Twig_Extension
{
protected $resources;
protected $templates;
protected $environment;
protected $themes;
public function __construct(array $resources = array())
{
$this->themes = new \SplObjectStorage();
$this->resources = $resources;
}
/**
* {@inheritdoc}
*/
public function initRuntime(\Twig_Environment $environment)
{
$this->environment = $environment;
}
/**
* Sets a theme for a given field.
*
* @param FieldInterface $field A FieldInterface instance
* @param array $resources An array of resources
*/
public function setTheme(FieldInterface $field, array $resources)
{
$this->themes->attach($field, $resources);
}
/**
* Returns the token parser instance to add to the existing list.
*
* @return array An array of Twig_TokenParser instances
*/
public function getTokenParsers()
{
return array(
// {% form_theme form "SomeBungle::widgets.twig" %}
new FormThemeTokenParser(),
);
}
public function getFunctions()
{
return array(
'form_enctype' => new \Twig_Function_Method($this, 'renderEnctype', array('is_safe' => array('html'))),
'form_field' => new \Twig_Function_Method($this, 'renderField', array('is_safe' => array('html'))),
'form_hidden' => new \Twig_Function_Method($this, 'renderHidden', array('is_safe' => array('html'))),
'form_errors' => new \Twig_Function_Method($this, 'renderErrors', array('is_safe' => array('html'))),
'form_label' => new \Twig_Function_Method($this, 'renderLabel', array('is_safe' => array('html'))),
'form_data' => new \Twig_Function_Method($this, 'renderData', array('is_safe' => array('html'))),
'form_row' => new \Twig_Function_Method($this, 'renderRow', array('is_safe' => array('html'))),
);
}
/**
* Renders the HTML enctype in the form tag, if necessary
*
* Example usage in Twig templates:
*
* <form action="..." method="post" {{ form_enctype(form) }}>
*
* @param Form $form The form for which to render the encoding type
*/
public function renderEnctype(Form $form)
{
return $form->isMultipart() ? 'enctype="multipart/form-data"' : '';
}
/**
* Renders a field row.
*
* @param FieldInterface $field The field to render as a row
*/
public function renderRow(FieldInterface $field)
{
return $this->render($field, 'field_row', array(
'child' => $field,
));
}
/**
* Renders the HTML for an individual form field
*
* Example usage in Twig:
*
* {{ form_field(field) }}
*
* You can pass attributes element during the call:
*
* {{ form_field(field, {'class': 'foo'}) }}
*
* Some fields also accept additional variables as parameters:
*
* {{ form_field(field, {}, {'separator': '+++++'}) }}
*
* @param FieldInterface $field The field to render
* @param array $attributes HTML attributes passed to the template
* @param array $parameters Additional variables passed to the template
* @param array|string $resources A resource or array of resources
*/
public function renderField(FieldInterface $field, array $attributes = array(), array $parameters = array(), $resources = null)
{
if (null !== $resources && !is_array($resources)) {
$resources = array($resources);
}
return $this->render($field, 'field', array(
'field' => $field,
'attr' => $attributes,
'params' => $parameters,
), $resources);
}
/**
* Renders all hidden fields of the given field group
*
* @param FormInterface $group The field group
* @param array $params Additional variables passed to the
* template
*/
public function renderHidden(FormInterface $group, array $parameters = array())
{
return $this->render($group, 'hidden', array(
'field' => $group,
'params' => $parameters,
));
}
/**
* Renders the errors of the given field
*
* @param FieldInterface $field The field to render the errors for
* @param array $params Additional variables passed to the template
*/
public function renderErrors(FieldInterface $field, array $parameters = array())
{
return $this->render($field, 'errors', array(
'field' => $field,
'params' => $parameters,
));
}
/**
* Renders the label of the given field
*
* @param FieldInterface $field The field to render the label for
* @param array $params Additional variables passed to the template
*/
public function renderLabel(FieldInterface $field, $label = null, array $parameters = array())
{
return $this->render($field, 'label', array(
'field' => $field,
'params' => $parameters,
'label' => null !== $label ? $label : ucfirst(strtolower(str_replace('_', ' ', $field->getKey()))),
));
}
/**
* Renders the widget data of the given field
*
* @param FieldInterface $field The field to render the data for
*/
public function renderData(FieldInterface $field)
{
return $field->getData();
}
protected function render(FieldInterface $field, $name, array $arguments, array $resources = null)
{
if ('field' === $name) {
list($name, $template) = $this->getWidget($field, $resources);
} else {
$template = $this->getTemplate($field, $name);
}
return $template->renderBlock($name, $arguments);
}
/**
* @param FieldInterface $field The field to get the widget for
* @param array $resources An array of template resources
* @return array
*/
protected function getWidget(FieldInterface $field, array $resources = null)
{
$class = get_class($field);
$templates = $this->getTemplates($field, $resources);
// find a template for the given class or one of its parents
do {
$parts = explode('\\', $class);
$c = array_pop($parts);
// convert the base class name (e.g. TextareaField) to underscores (e.g. textarea_field)
$underscore = strtolower(preg_replace(array('/([A-Z]+)([A-Z][a-z])/', '/([a-z\d])([A-Z])/'), array('\\1_\\2', '\\1_\\2'), strtr($c, '_', '.')));
if (isset($templates[$underscore])) {
return array($underscore, $templates[$underscore]);
}
} while (false !== $class = get_parent_class($class));
throw new \RuntimeException(sprintf('Unable to render the "%s" field.', $field->getKey()));
}
protected function getTemplate(FieldInterface $field, $name, array $resources = null)
{
$templates = $this->getTemplates($field, $resources);
return $templates[$name];
}
protected function getTemplates(FieldInterface $field, array $resources = null)
{
// templates are looked for in the following resources:
// * resources provided directly into the function call
// * resources from the themes (and its parents)
// * default resources
// defaults
$all = $this->resources;
// themes
$parent = $field;
do {
if (isset($this->themes[$parent])) {
$all = array_merge($all, $this->themes[$parent]);
}
} while ($parent = $parent->getParent());
// local
$all = array_merge($all, null !== $resources ? (array) $resources : array());
$templates = array();
foreach ($all as $resource) {
if (!$resource instanceof \Twig_Template) {
$resource = $this->environment->loadTemplate($resource);
}
$blocks = array();
foreach ($this->getBlockNames($resource) as $name) {
$blocks[$name] = $resource;
}
$templates = array_replace($templates, $blocks);
}
return $templates;
}
protected function getBlockNames($resource)
{
$names = $resource->getBlockNames();
$parent = $resource;
while (false !== $parent = $parent->getParent(array())) {
$names = array_merge($names, $parent->getBlockNames());
}
return array_unique($names);
}
/**
* Returns the name of the extension.
*
* @return string The extension name
*/
public function getName()
{
return 'form';
}
}