Skip to content

Commit 88f7b5e

Browse files
committed
[#1806] Minor tweaks to new form type extension entry
1 parent 834b1ac commit 88f7b5e

File tree

1 file changed

+27
-38
lines changed

1 file changed

+27
-38
lines changed

cookbook/form/create_form_type_extension.rst

Lines changed: 27 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
single: Form; Form type extension
33

44
How to Create a Form Type Extension
5-
====================================
5+
===================================
66

77
:doc:`Custom form field types<create_custom_field_type>` are great when
88
you need field types with a specific purpose, such as a gender selector,
@@ -12,7 +12,7 @@ But sometimes, you don't really need to add new field types - you want
1212
to add features on top of existing types. This is where form type
1313
extensions come in.
1414

15-
Form type extensions have 2 main use cases:
15+
Form type extensions have 2 main use-cases:
1616

1717
#. You want to add a **generic feature to several types** (such as
1818
adding a "help" text to every field type);
@@ -33,24 +33,23 @@ to a file. Your ``Media`` form uses a file type, but when editing the entity,
3333
you would like to see its image automatically rendered next to the file
3434
input.
3535

36-
You could of course do this by customizing how this field is rendered in a template. But field
37-
type extensions allow you to do this in a nice DRY fashion.
36+
You could of course do this by customizing how this field is rendered in a
37+
template. But field type extensions allow you to do this in a nice DRY fashion.
3838

3939
Defining the Form Type Extension
40-
---------------------------------
40+
--------------------------------
4141

4242
Your first task will be to create the form type extension class. Let's
43-
call it ``ImageTypeExtension``. You will store the class in a file called
44-
``ImageTypeExtension.php``, in the ``<BundleName>\Form\Type`` directory.
43+
call it ``ImageTypeExtension``. By standard, form extensions usually live
44+
in the ``Form\Extension`` directory of one of your bundles.
4545

4646
When creating a form type extension, you can either implement the
47-
:class:`Symfony\\Component\\Form\\FormTypeExtensionInterface` interface,
47+
:class:`Symfony\\Component\\Form\\FormTypeExtensionInterface` interface
4848
or extend the :class:`Symfony\\Component\\Form\\AbstractTypeExtension`
49-
class. Most of the time, you will end up extending the abstract class.
50-
That's what you will do in this tutorial::
49+
class. In most cases, it's easier to extend the abstract class::
5150

52-
// src/Acme/DemoBundle/Form/Type/ImageTypeExtension.php
53-
namespace Acme\DemoBundle\Form\Type;
51+
// src/Acme/DemoBundle/Form/Extension/ImageTypeExtension.php
52+
namespace Acme\DemoBundle\Form\Extension;
5453

5554
use Symfony\Component\Form\AbstractTypeExtension;
5655

@@ -65,7 +64,6 @@ That's what you will do in this tutorial::
6564
{
6665
return 'file';
6766
}
68-
6967
}
7068

7169
The only method you **must** implement is the ``getExtendedType`` function.
@@ -128,37 +126,28 @@ The ``alias`` key of the tag is the type of field that this extension should
128126
be applied to. In your case, as you want to extend the ``file`` field type,
129127
you will use ``file`` as an alias.
130128

131-
Adding the extension business logic
129+
Adding the extension Business Logic
132130
-----------------------------------
133131

134-
The goal of your extension is to display a nice image next to file inputs
132+
The goal of your extension is to display nice images next to file inputs
135133
(when the underlying model contains images). For that purpose, let's assume
136134
that you use an approach similar to the one described in
137135
:doc:`How to handle File Uploads with Doctrine</cookbook/doctrine/file_uploads>`:
138136
you have a Media model with a file property (corresponding to the file field
139137
in the form) and a path property (corresponding to the image path in the
140-
database).
141-
142-
.. code-block:: php
138+
database)::
143139

144140
// src/Acme/DemoBundle/Entity/Media.php
145141
namespace Acme\DemoBundle\Entity;
146142

147-
use Doctrine\ORM\Mapping as ORM;
148143
use Symfony\Component\Validator\Constraints as Assert;
149144

150-
/**
151-
* @ORM\Entity
152-
* @ORM\Table
153-
*/
154145
class Media
155146
{
156147
// ...
157148

158149
/**
159-
* @var string
160-
*
161-
* @ORM\Column(name="path", type="string", length=255)
150+
* @var string The path - typically stored in the database
162151
*/
163152
private $path;
164153

@@ -182,7 +171,8 @@ database).
182171
return $webPath;
183172
}
184173

185-
Your form type extension class will need to do two things:
174+
Your form type extension class will need to do two things in order to extend
175+
the ``file`` form type:
186176

187177
#. Override the ``getDefaultOptions`` method in order to add an image_path
188178
option;
@@ -192,12 +182,10 @@ Your form type extension class will need to do two things:
192182
The logic is the following: when adding a form field of type ``file``,
193183
you will be able to specify a new option: ``image_path``. This option will
194184
tell the file field how to get the actual image url in order to display
195-
it in the view.
185+
it in the view::
196186

197-
.. code-block:: php
198-
199-
// src/Acme/DemoBundle/Form/Type/ImageTypeExtension.php
200-
namespace Acme\DemoBundle\Form\Type;
187+
// src/Acme/DemoBundle/Form/Extension/ImageTypeExtension.php
188+
namespace Acme\DemoBundle\Form\Extension;
201189

202190
use Symfony\Component\Form\AbstractTypeExtension;
203191
use Symfony\Component\Form\FormBuilder;
@@ -253,22 +241,23 @@ it in the view.
253241

254242
$propertyPath = new PropertyPath($form->getAttribute('image_path'));
255243
$imageUrl = $propertyPath->getValue($parentData);
244+
// set an "image_url" variable that will be available when rendering this field
256245
$view->set('image_url', $imageUrl);
257246
}
258247
}
259248

260249
}
261250

262-
Override the file widget template fragment
251+
Override the File Widget Template Fragment
263252
------------------------------------------
264253

265254
Each field type is rendered by a template fragment. Those template fragments
266-
can be overridden in order to customize form rendering; for more information,
255+
can be overridden in order to customize form rendering. For more information,
267256
you can refer to the :ref:`cookbook-form-customization-form-themes` article.
268257

269258
In your extension class, you have added a new variable (``image_url``), but
270259
you still need to take advantage of this new variable in your templates.
271-
You need to override the ``file_widget`` block:
260+
Specifically, you need to override the ``file_widget`` block:
272261

273262
.. configuration-block::
274263

@@ -298,7 +287,7 @@ You need to override the ``file_widget`` block:
298287

299288
.. note::
300289

301-
You will need to change your config file or to explicitly specify how
290+
You will need to change your config file or explicitly specify how
302291
you want your form to be themed in order for Symfony to use your overridden
303292
block. See :ref:`cookbook-form-customization-form-themes` for more
304293
information.
@@ -308,10 +297,10 @@ Using the Form Type Extension
308297

309298
From now on, when adding a field of type ``file`` in your form, you can
310299
specify an ``image_path`` option that will be used to display an image
311-
next to the file field. As an example::
300+
next to the file field. For example::
312301

313302
// src/Acme/DemoBundle/Form/Type/MediaType.php
314-
namespace Acme\DemoBundle\Form;
303+
namespace Acme\DemoBundle\Form\Type;
315304

316305
use Symfony\Component\Form\AbstractType;
317306
use Symfony\Component\Form\FormBuilder;

0 commit comments

Comments
 (0)