Skip to content

Commit 28ea82b

Browse files
committed
[#1806] Updating the new form extensions entry to 2.1 using #1691 and @pvanliefland as my guide
1 parent 0e9ce76 commit 28ea82b

File tree

1 file changed

+12
-26
lines changed

1 file changed

+12
-26
lines changed

cookbook/form/create_form_type_extension.rst

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,9 @@ to override one of the following methods:
8383

8484
* ``buildView()``
8585

86-
* ``getDefaultOptions()``
86+
* ``setDefaultOptions()``
8787

88-
* ``getAllowedOptionValues()``
89-
90-
* ``buildViewBottomUp()``
88+
* ``finishView()``
9189

9290
For more information on what those methods do, you can refer to the
9391
:doc:`Creating Custom Field Types</cookbook/form/create_custom_field_type>`
@@ -174,7 +172,7 @@ database)::
174172
Your form type extension class will need to do two things in order to extend
175173
the ``file`` form type:
176174

177-
#. Override the ``getDefaultOptions`` method in order to add an image_path
175+
#. Override the ``setDefaultOptions`` method in order to add an image_path
178176
option;
179177
#. Override the ``buildForm`` and ``buildView`` methods in order to pass the image
180178
url to the view.
@@ -192,6 +190,7 @@ it in the view::
192190
use Symfony\Component\Form\FormView;
193191
use Symfony\Component\Form\FormInterface;
194192
use Symfony\Component\Form\Util\PropertyPath;
193+
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
195194

196195
class ImageTypeExtension extends AbstractTypeExtension
197196
{
@@ -208,24 +207,11 @@ it in the view::
208207
/**
209208
* Add the image_path option
210209
*
211-
* @param array $options
210+
* @param \Symfony\Component\OptionsResolver\OptionsResolverInterface $resolver
212211
*/
213-
public function getDefaultOptions(array $options)
212+
public function setDefaultOptions(OptionsResolverInterface $resolver)
214213
{
215-
return array('image_path' => null);
216-
}
217-
218-
/**
219-
* Store the image_path option as a builder attribute
220-
*
221-
* @param \Symfony\Component\Form\FormBuilder $builder
222-
* @param array $options
223-
*/
224-
public function buildForm(FormBuilder $builder, array $options)
225-
{
226-
if (null !== $options['image_path']) {
227-
$builder->setAttribute('image_path', $options['image_path']);
228-
}
214+
$resolver->setOptional(array('image_path'));
229215
}
230216

231217
/**
@@ -236,10 +222,10 @@ it in the view::
236222
*/
237223
public function buildView(FormView $view, FormInterface $form)
238224
{
239-
if ($form->hasAttribute('image_path')) {
225+
if (array_key_exists('image_path', $options)) {
240226
$parentData = $form->getParent()->getData();
241227

242-
$propertyPath = new PropertyPath($form->getAttribute('image_path'));
228+
$propertyPath = new PropertyPath($options['image_path']);
243229
$imageUrl = $propertyPath->getValue($parentData);
244230
// set an "image_url" variable that will be available when rendering this field
245231
$view->set('image_url', $imageUrl);
@@ -269,7 +255,7 @@ Specifically, you need to override the ``file_widget`` block:
269255
{% block file_widget %}
270256
{% spaceless %}
271257

272-
{{ block('field_widget') }}
258+
{{ block('form_widget') }}
273259
{% if image_url is not null %}
274260
<img src="{{ asset(image_url) }}"/>
275261
{% endif %}
@@ -303,11 +289,11 @@ next to the file field. For example::
303289
namespace Acme\DemoBundle\Form\Type;
304290

305291
use Symfony\Component\Form\AbstractType;
306-
use Symfony\Component\Form\FormBuilder;
292+
use Symfony\Component\Form\FormBuilderInterface;
307293

308294
class MediaType extends AbstractType
309295
{
310-
public function buildForm(FormBuilder $builder, array $options)
296+
public function buildForm(FormBuilderInterface $builder, array $options)
311297
{
312298
$builder
313299
->add('name', 'text')

0 commit comments

Comments
 (0)