From 4a926c4e694e877f370734710606b67f4bd462aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Paris?= Date: Sun, 5 Nov 2017 20:38:04 +0100 Subject: [PATCH] Deprecate the form mapping feature --- UPGRADE-3.x.md | 12 ++ docs/reference/api.rst | 4 +- docs/reference/form_types.rst | 151 ++++++------------ docs/reference/installation.rst | 5 +- src/Command/SonataListFormMappingCommand.php | 3 + .../Compiler/FormFactoryCompilerPass.php | 3 + .../SonataCoreExtension.php | 4 + .../DependencyInjectionExtension.php | 2 + .../SonataCoreExtensionTest.php | 7 +- 9 files changed, 82 insertions(+), 109 deletions(-) diff --git a/UPGRADE-3.x.md b/UPGRADE-3.x.md index 21ef6656..75e468fb 100644 --- a/UPGRADE-3.x.md +++ b/UPGRADE-3.x.md @@ -1,6 +1,18 @@ UPGRADE 3.x =========== +Using the form mapping feature is now deprecated. You should use FQCNs +everywhere form names are used, and disable this feature with the following +piece of configuration: + +```yaml +# config.yml +sonata_core: + form: + mapping: + enabled: false +``` + UPGRADE FROM 3.4 to 3.5 ======================= diff --git a/docs/reference/api.rst b/docs/reference/api.rst index 57e856c8..80c28166 100644 --- a/docs/reference/api.rst +++ b/docs/reference/api.rst @@ -13,9 +13,11 @@ client only sent partial data to update an entity. getCategory($id) : null; - $form = $this->formFactory->createNamed(null, 'sonata_classification_api_form_category', $category, array( + $form = $this->formFactory->createNamed(null, ApiCategoryType::class, $category, array( 'csrf_protection' => false )); diff --git a/docs/reference/form_types.rst b/docs/reference/form_types.rst index 7640a559..fd85a52c 100644 --- a/docs/reference/form_types.rst +++ b/docs/reference/form_types.rst @@ -6,80 +6,8 @@ Form Types The bundle comes with some handy form types. - -Symfony3 Supports ------------------ - -In order to help the migration path toward Symfony3, the bundle restores the Symfony2.X behavior of string code usage for the form type declaration. -Each bundles can register a mapping with Symfony3 boot. - -.. code-block:: php - - registerFormMapping(); - } - - /** - * {@inheritdoc} - */ - public function boot() - { - $this->registerFormMapping(); - } - - /** - * Register form mapping information - */ - public function registerFormMapping() - { - FormHelper::registerFormTypeMapping(array( - 'sonata_type_admin' => 'Sonata\AdminBundle\Form\Type\AdminType', // the form mapping - )); - - FormHelper::registerFormExtensionMapping('choice', array( // the extended mapping for the choice type - 'sonata.admin.form.extension.choice', - )); - } - } - -You can either alter your bundle to configure these mappings or you can add the mapping as configuration settings. Values provided in configuration will overwrite default values: - -.. code-block: yaml - - sonata_core: - form: - mapping: - enabled: true # (default value) - type: - sonata_type_admin: Sonata\AdminBundle\Form\Type\AdminType - - extension: - choice: - - sonata.admin.form.extension.choice - -If you are lazy, you can get the mapping with the command: - -.. code-block: yaml - - app/console sonata:core:form-mapping -f yaml|php - - - -DoctrineORMSerializationType ----------------------------- +Sonata\CoreBundle\Form\Type\DoctrineORMSerializationType +-------------------------------------------------------- This form type reads ``JMSSerializer`` serialization class metadata and uses ``Doctrine`` ORM entity metadata to generate form fields and correct types. @@ -115,8 +43,8 @@ The service definition should contain the following arguments: -sonata_type_immutable_array ---------------------------- +Sonata\CoreBundle\Form\Type\ImmutableArrayType +---------------------------------------------- The ``Immutable Array`` allows you to edit an array property by defining a type per key. @@ -159,13 +87,14 @@ Now, the property can be edited by setting a type for each type: add('options', 'sonata_type_immutable_array', array( + ->add('options', ImmutableArrayType::class, array( 'keys' => array( array('ttl', 'text', array('required' => false)), array('redirect', 'url', array('required' => true)), @@ -178,16 +107,16 @@ Now, the property can be edited by setting a type for each type: // ... } -sonata_type_boolean -------------------- +Sonata\CoreBundle\Form\Type\BooleanType +--------------------------------------- The ``boolean`` type is a specialized ``ChoiceType``, where the list of choices is locked to *yes* and *no*. Note that for backward compatibility reasons, it will set your value to *1* for *yes* and to *2* for *no*. If you want to map to a boolean value, just set the option ``transform`` to true. For instance, you need to do so when mapping to a doctrine boolean. -sonata_type_translatable_choice -------------------------------- +Sonata\CoreBundle\Form\Type\TranslatableChoiceType +-------------------------------------------------- The translatable type is a specialized ``ChoiceType`` where the choices values are translated with the Symfony Translator component. @@ -222,13 +151,14 @@ The type has one extra parameter: add('deliveryStatus', 'sonata_type_translatable_choice', array( + ->add('deliveryStatus', TranslatableChoiceType::class, array( 'choices' => Delivery::getStatusList(), 'catalogue' => 'SonataOrderBundle' )) @@ -243,8 +173,8 @@ The type has one extra parameter: For more information, you can check the official `ChoiceType documentation`_ . -sonata_type_collection ----------------------- +Sonata\CoreBundle\Form\Type\CollectionType +------------------------------------------ The ``Collection Type`` is meant to handle creation and editing of model collections. Rows can be added and deleted, and your model abstraction layer may @@ -255,13 +185,14 @@ to the underlying forms. add('sales', 'sonata_type_collection', array( + ->add('sales', CollectionType::class, array( // Prevents the "Delete" option from being displayed 'type_options' => array('delete' => false) @@ -296,8 +227,8 @@ pre_bind_data_callback: **TIP**: A jQuery event is fired after a row has been added (``sonata-admin-append-form-element``). You can listen to this event to trigger custom javascript (eg: add a calendar widget to a newly added date field) -StatusType ----------- +Sonata\CoreBundle\Form\Type\StatusType +-------------------------------------- The ``StatusType`` is not available as a service. However, you can use it to declare your own type to render a choice of status. @@ -330,7 +261,7 @@ This can be done by declaring a new service: .. code-block:: xml - + %sonata.order.order.class% getStatusList @@ -343,13 +274,14 @@ And the type can now be used: add('deliveryStatus', 'sonata_order_status') + ->add('deliveryStatus', OrderStatusType::class) // ... ; } @@ -358,12 +290,12 @@ And the type can now be used: .. warning:: ``StatusType`` cannot be used directly with Symfony3.0, you need to extends the class ``BaseStatusType`` - with an empty class to have an unique FQCN. + with an empty class to have a unique FQCN. -sonata_type_date_picker and sonata_type_datetime_picker -------------------------------------------------------- +Sonata\CoreBundle\Form\Type\DatePickerType and Sonata\CoreBundle\Form\Type\DateTimePickerType +--------------------------------------------------------------------------------------------- Those types integrate `Eonasdan's Bootstrap datetimepicker`_ into a Symfony form. They both are available as services, and inherit from ``date`` and ``datetime`` default form types. @@ -408,16 +340,18 @@ Finally, in your form, you may use the form type as follows: add('publicationDateStart', 'sonata_type_datetime_picker') + ->add('publicationDateStart', DateTimePickerType::class) // or sonata_type_date_picker if you don't need the time - ->add('publicationDateStart', 'sonata_type_date_picker') + ->add('publicationDateStart', DatePickerType::class) // ... ; @@ -430,13 +364,15 @@ Many of the `standard date picker options`_ are available by adding options with add('publicationDateStart', 'sonata_type_datetime_picker', array( + ->add('publicationDateStart', DateTimePickerType::class, array( 'dp_side_by_side' => true, 'dp_use_current' => false, 'dp_use_seconds' => false, @@ -447,7 +383,7 @@ Many of the `standard date picker options`_ are available by adding options with )) // or sonata_type_date_picker if you don't need the time - ->add('publicationDateStart', 'sonata_type_date_picker', array( + ->add('publicationDateStart', DatePickerType::class, array( 'dp_use_current' => false, )) ; @@ -458,18 +394,22 @@ If you look in the classes ``DateTimePickerType.php`` and ``BasePickerType.php`` In addition to these standard options, there is also the option ``datepicker_use_button`` which, when used, will change the widget so that the datepicker icon is not shown and the pop-up datepicker is invoked simply by clicking on the date input. -sonata_type_date_range_picker and sonata_type_datetime_range_picker -------------------------------------------------------------------- +Sonata\CoreBundle\Form\Type\DateRangePickerType and DateTimeRangePickerType +--------------------------------------------------------------------------- -``sonata_type_date_range_picker`` and ``sonata_type_datetime_range_picker`` extends the basic range form field types (``sonata_type_date_range`` and ``sonata_type_datetime_range``). +Those types extend the basic range form field types +(``Sonata\CoreBundle\Form\Type\DateRangeType`` and +``Sonata\CoreBundle\Form\Type\DateTimeRangeType``). You can use them if you need datetime picker in datetime range filters. -Example with ``doctrine_orm_date_range`` filter: +Example with ``Sonata\DoctrineORMAdminBundle\Filter\DateRangeFilter`` filter: .. code-block:: php add('createdAt', 'doctrine_orm_date_range', array( - 'field_type' => 'sonata_type_date_range_picker', + ->add('createdAt', DateRangeFilter::class, array( + 'field_type' => DateRangeType::class, )) // ... ; @@ -487,8 +427,8 @@ Example with ``doctrine_orm_date_range`` filter: // ... } -sonata_type_color ------------------------- +Sonata\CoreBundle\Form\Type\ColorType +------------------------------------- This is HTML5 input type color. @@ -511,13 +451,14 @@ Finally, in your form, you may use the form type as follows: add('color', 'sonata_type_color') + ->add('color', ColorType::class) // ... ; } diff --git a/docs/reference/installation.rst b/docs/reference/installation.rst index 2c41a440..775d3af4 100644 --- a/docs/reference/installation.rst +++ b/docs/reference/installation.rst @@ -37,7 +37,10 @@ Configuration # app/config/config.yml - sonata_core: ~ + sonata_core: + form: + mapping: + enabled: false When using bootstrap, some widgets need to be wrapped in a special ``div`` element depending on whether you are using the standard style for your forms or the diff --git a/src/Command/SonataListFormMappingCommand.php b/src/Command/SonataListFormMappingCommand.php index bf38c3da..89da125e 100644 --- a/src/Command/SonataListFormMappingCommand.php +++ b/src/Command/SonataListFormMappingCommand.php @@ -17,6 +17,9 @@ use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\HttpKernel\Kernel; +/** + * @deprecated since 3.x, to be removed in 4.0, the form mapping feature should be disabled. + */ class SonataListFormMappingCommand extends ContainerAwareCommand { /** diff --git a/src/DependencyInjection/Compiler/FormFactoryCompilerPass.php b/src/DependencyInjection/Compiler/FormFactoryCompilerPass.php index 8980beb2..ab3cdcc5 100644 --- a/src/DependencyInjection/Compiler/FormFactoryCompilerPass.php +++ b/src/DependencyInjection/Compiler/FormFactoryCompilerPass.php @@ -14,6 +14,9 @@ use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\FormPass; use Symfony\Component\DependencyInjection\ContainerBuilder; +/** + * @deprecated since 3.x, to be removed in 4.0, the form mapping feature should be disabled. + */ class FormFactoryCompilerPass extends FormPass { /** diff --git a/src/DependencyInjection/SonataCoreExtension.php b/src/DependencyInjection/SonataCoreExtension.php index 5e2f7465..9bafa65e 100644 --- a/src/DependencyInjection/SonataCoreExtension.php +++ b/src/DependencyInjection/SonataCoreExtension.php @@ -116,6 +116,10 @@ public function configureFormFactory(ContainerBuilder $container, array $config) return; } + @trigger_error( + 'Relying on the form mapping feature is deprecated since 3.x and will be removed in 4.0. Please set the "sonata_core.form.mapping.enabled" configuration node to false to avoid this message.', + E_USER_DEPRECATED + ); $container->setParameter('sonata.core.form.mapping.type', $config['form']['mapping']['type']); $container->setParameter('sonata.core.form.mapping.extension', $config['form']['mapping']['extension']); diff --git a/src/Form/Extension/DependencyInjectionExtension.php b/src/Form/Extension/DependencyInjectionExtension.php index 94d81fd9..406ef654 100644 --- a/src/Form/Extension/DependencyInjectionExtension.php +++ b/src/Form/Extension/DependencyInjectionExtension.php @@ -20,6 +20,8 @@ /** * This proxy class help to keep BC code with < SF2.8 form behavior by restoring * the type as a code and not as a class. + * + * @deprecated since 3.x, to be removed in 4.0, the form mapping feature should be disabled. */ class DependencyInjectionExtension implements FormExtensionInterface { diff --git a/tests/DependencyInjection/SonataCoreExtensionTest.php b/tests/DependencyInjection/SonataCoreExtensionTest.php index ac551aee..8012d1c2 100644 --- a/tests/DependencyInjection/SonataCoreExtensionTest.php +++ b/tests/DependencyInjection/SonataCoreExtensionTest.php @@ -18,7 +18,7 @@ class SonataCoreExtensionTest extends AbstractExtensionTestCase { public function testAfterLoadingTheWrappingParameterIsSet() { - $this->load(); + $this->load(['form' => ['mapping' => ['enabled' => false]]]); $this->assertContainerBuilderHasParameter( 'sonata.core.form_type' ); @@ -32,7 +32,10 @@ public function testAfterLoadingTheWrappingParameterIsSet() public function testHorizontalFormTypeMeansNoWrapping() { - $this->load(['form_type' => 'horizontal']); + $this->load([ + 'form' => ['mapping' => ['enabled' => false]], + 'form_type' => 'horizontal', + ]); $this->assertContainerBuilderHasParameter( 'sonata.core.form_type' );