2222Defining the Form Type Extension
2323--------------------------------
2424
25- First, create the form type extension class::
25+ First, create the form type extension class extending from
26+ :class: `Symfony\\ Component\\ Form\\ AbstractTypeExtension ` (you can implement
27+ :class: `Symfony\\ Component\\ Form\\ FormTypeExtensionInterface ` instead if you prefer)::
2628
2729 // src/Form/Extension/ImageTypeExtension.php
2830 namespace App\Form\Extension;
@@ -33,29 +35,26 @@ First, create the form type extension class::
3335 class ImageTypeExtension extends AbstractTypeExtension
3436 {
3537 /**
36- * Returns the name of the type being extended.
37- *
38- * @return string The name of the type being extended
38+ * Return the class of the type being extended.
3939 */
40- public function getExtendedType()
40+ public static function getExtendedTypes(): iterable
4141 {
42- // use FormType::class to modify (nearly) every field in the system
43- return FileType::class;
42+ // return FormType::class to modify (nearly) every field in the system
43+ return array( FileType::class) ;
4444 }
4545 }
4646
47- The only method you **must ** implement is the `` getExtendedType () `` function.
48- This is used to configure *which * field or field types you want to modify.
47+ The only method you **must ** implement is `` getExtendedTypes () ``, which is used
48+ to configure *which * field types you want to modify.
4949
50- In addition to the `` getExtendedType() `` function, you will probably want
51- to override one of the following methods:
50+ .. versionadded :: 4.2
51+ The `` getExtendedTypes() `` method was introduced in Symfony 4.2.
5252
53- * `` buildForm() ``
53+ Depending on your use case, you may need to override some of the following methods:
5454
55+ * ``buildForm() ``
5556* ``buildView() ``
56-
5757* ``configureOptions() ``
58-
5958* ``finishView() ``
6059
6160For more information on what those methods do, see the
@@ -64,61 +63,23 @@ For more information on what those methods do, see the
6463Registering your Form Type Extension as a Service
6564-------------------------------------------------
6665
67- The next step is to make Symfony aware of your extension. Do this by registering
68- your class as a service and using the ``form.type_extension `` tag:
69-
70- .. configuration-block ::
71-
72- .. code-block :: yaml
73-
74- # config/services.yaml
75- services :
76- # ...
77-
78- App\Form\Extension\ImageTypeExtension :
79- tags :
80- - { name: form.type_extension, extended_type: Symfony\Component\Form\Extension\Core\Type\FileType }
81-
82- .. code-block :: xml
83-
84- <!-- config/services.xml -->
85- <?xml version =" 1.0" encoding =" UTF-8" ?>
86- <container xmlns =" http://symfony.com/schema/dic/services"
87- xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
88- xsi : schemaLocation =" http://symfony.com/schema/dic/services
89- http://symfony.com/schema/dic/services/services-1.0.xsd" >
90-
91- <services >
92- <service id =" App\Form\Extension\ImageTypeExtension" >
93- <tag name =" form.type_extension" extended-type =" Symfony\Component\Form\Extension\Core\Type\FileType" />
94- </service >
95- </services >
96- </container >
97-
98- .. code-block :: php
99-
100- // config/services.php
101- use App\Form\Extension\ImageTypeExtension;
102- use Symfony\Component\Form\Extension\Core\Type\FileType;
103-
104- $container->autowire(ImageTypeExtension::class)
105- ->addTag('form.type_extension', array(
106- 'extended_type' => FileType::class
107- ))
108- ;
109-
110- The ``extended_type `` key of the tag must match the class you're returning from
111- the ``getExtendedType() `` method. As *soon * as you do this, any method that you've
112- overridden (e.g. ``buildForm() ``) will be called whenever *any * field of the given
113- type (``FileType ``) is built. Let's see an example next.
66+ Form type extensions must be registered as services and :doc: `tagged </service_container/tags >`
67+ with the ``form.type_extension `` tag. If you're using the
68+ :ref: `default services.yaml configuration <service-container-services-load-example >`,
69+ this is already done for you, thanks to :ref: `autoconfiguration <services-autoconfigure >`.
11470
11571.. tip ::
11672
117- There is an optional tag attribute called ``priority ``, which
118- defaults to ``0 `` and controls the order in which the form
119- type extensions are loaded (the higher the priority, the earlier
120- an extension is loaded). This is useful when you need to guarantee
121- that one extension is loaded before or after another extension.
73+ There is an optional tag attribute called ``priority ``, which defaults to
74+ ``0 `` and controls the order in which the form type extensions are loaded
75+ (the higher the priority, the earlier an extension is loaded). This is
76+ useful when you need to guarantee that one extension is loaded before or
77+ after another extension. Using this attribute requires you to add the
78+ service configuration explicitly.
79+
80+ Once the extension is registered, any method that you've overridden (e.g.
81+ ``buildForm() ``) will be called whenever *any * field of the given type
82+ (``FileType ``) is built.
12283
12384Adding the extension Business Logic
12485-----------------------------------
@@ -175,9 +136,10 @@ For example::
175136
176137 class ImageTypeExtension extends AbstractTypeExtension
177138 {
178- public function getExtendedType()
139+ public static function getExtendedTypes(): iterable
179140 {
180- return FileType::class;
141+ // return FormType::class to modify (nearly) every field in the system
142+ return array(FileType::class);
181143 }
182144
183145 public function configureOptions(OptionsResolver $resolver)
@@ -278,3 +240,25 @@ would apply to all of these (notable exceptions are the ``ButtonType`` form
278240types). Also keep in mind that if you created (or are using) a *custom * form type,
279241it's possible that it does *not * extend ``FormType ``, and so your form type extension
280242may not be applied to it.
243+
244+ Another option is to return multiple form types in the ``getExtendedTypes() ``
245+ method to extend all of them::
246+
247+ // ...
248+ use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
249+ use Symfony\Component\Form\Extension\Core\Type\DateType;
250+ use Symfony\Component\Form\Extension\Core\Type\TimeType;
251+
252+ class DateTimeExtension extends AbstractTypeExtension
253+ {
254+ // ...
255+
256+ public static function getExtendedTypes(): iterable
257+ {
258+ return array(DateTimeType::class, DateType::class, TimeType::class);
259+ }
260+ }
261+
262+ .. versionadded :: 4.2
263+ The feature to extend multiple form types using a single extension class
264+ was introduced in Symfony 4.2.
0 commit comments