From f2e208d44b92f1105a057a097bfecbaaf6407365 Mon Sep 17 00:00:00 2001 From: Bertrand Dunogier Date: Fri, 13 Dec 2019 17:50:46 +0100 Subject: [PATCH] Prototyped a more advanced query type parameters UI --- .../Resources/config/services/ezplatform.yml | 10 ++++ .../Resources/views/field_types.html.twig | 6 ++ .../FieldDefinitionParametersSubscriber.php | 55 +++++++++++++++++++ .../Form/FieldDefinitionParametersType.php | 39 +++++++++++++ .../Mapper/ParametersTransformer.php | 31 ----------- src/eZ/FieldType/Mapper/QueryFormMapper.php | 22 +++----- 6 files changed, 117 insertions(+), 46 deletions(-) create mode 100644 src/eZ/FieldType/Form/EventSubscriber/FieldDefinitionParametersSubscriber.php create mode 100644 src/eZ/FieldType/Form/FieldDefinitionParametersType.php delete mode 100644 src/eZ/FieldType/Mapper/ParametersTransformer.php diff --git a/src/Symfony/Resources/config/services/ezplatform.yml b/src/Symfony/Resources/config/services/ezplatform.yml index 6d7c93f..a09fd1a 100644 --- a/src/Symfony/Resources/config/services/ezplatform.yml +++ b/src/Symfony/Resources/config/services/ezplatform.yml @@ -40,3 +40,13 @@ services: $views: { field: '%ezcontentquery_field_view%', item: '%ezcontentquery_item_view%' } tags: - { name: kernel.event_subscriber } + + EzSystems\EzPlatformQueryFieldType\eZ\FieldType\Form\FieldDefinitionParametersType: + arguments: + $parametersSubscriber: '@EzSystems\EzPlatformQueryFieldType\eZ\FieldType\Form\EventSubscriber\FieldDefinitionParametersSubscriber' + tags: + - { name: form.type } + + EzSystems\EzPlatformQueryFieldType\eZ\FieldType\Form\EventSubscriber\FieldDefinitionParametersSubscriber: + tags: + - { name: kernel.event_subscriber } diff --git a/src/Symfony/Resources/views/field_types.html.twig b/src/Symfony/Resources/views/field_types.html.twig index bcabc5f..5d357e3 100644 --- a/src/Symfony/Resources/views/field_types.html.twig +++ b/src/Symfony/Resources/views/field_types.html.twig @@ -5,6 +5,8 @@ {{- form_label(form.QueryType) -}} {{- form_errors(form.QueryType) -}} {{- form_widget(form.QueryType) -}} + + {{- form_widget(form.SetQueryType) -}}
@@ -14,8 +16,12 @@
+ {% if form.Parameters.children|length > 0 %} {{- form_label(form.Parameters) -}} {{- form_errors(form.Parameters) -}} {{- form_widget(form.Parameters) -}} + {% else %} + To edit the parameters, select a query type in the dropdown above and hit 'Set'. + {% endif %}
{% endblock %} diff --git a/src/eZ/FieldType/Form/EventSubscriber/FieldDefinitionParametersSubscriber.php b/src/eZ/FieldType/Form/EventSubscriber/FieldDefinitionParametersSubscriber.php new file mode 100644 index 0000000..b2d7b2c --- /dev/null +++ b/src/eZ/FieldType/Form/EventSubscriber/FieldDefinitionParametersSubscriber.php @@ -0,0 +1,55 @@ +queryTypeRegistry = $queryTypeRegistry; + } + + public static function getSubscribedEvents() + { + return [FormEvents::PRE_SET_DATA => 'addParametersFormFields']; + } + + public function addParametersFormFields(FormEvent $event) + { + $data = $event->getData(); + if ($data === null) { + return; + } + + $queryTypeIdentifier = $event->getForm()->getConfig()->getOption('query_type'); + if ($queryTypeIdentifier === null) { + return; + } + + $queryType = $this->queryTypeRegistry->getQueryType($queryTypeIdentifier); + foreach ($queryType->getSupportedParameters() as $parameter) { + $event->getForm()->add( + $parameter, + Type\TextType::class, + [ + 'label' => $parameter, + 'property_path' => sprintf('[Parameters][%s]', $parameter), + 'required' => false, + ] + ); + } + } +} diff --git a/src/eZ/FieldType/Form/FieldDefinitionParametersType.php b/src/eZ/FieldType/Form/FieldDefinitionParametersType.php new file mode 100644 index 0000000..cd036c7 --- /dev/null +++ b/src/eZ/FieldType/Form/FieldDefinitionParametersType.php @@ -0,0 +1,39 @@ +parametersSubscriber = $parametersSubscriber; + } + + public function getParent() + { + return Type\FormType::class; + } + + public function configureOptions(OptionsResolver $resolver) + { + $resolver->setDefault('query_type', null); + } + + public function buildForm(FormBuilderInterface $builder, array $options) + { + $builder->addEventSubscriber($this->parametersSubscriber); + } +} diff --git a/src/eZ/FieldType/Mapper/ParametersTransformer.php b/src/eZ/FieldType/Mapper/ParametersTransformer.php deleted file mode 100644 index 19405dd..0000000 --- a/src/eZ/FieldType/Mapper/ParametersTransformer.php +++ /dev/null @@ -1,31 +0,0 @@ -getConfig()->getFormFactory()->createBuilder() - ->create( - 'Parameters', - Type\TextareaType::class, - [ - 'label' => 'Parameters', - 'property_path' => 'fieldSettings[Parameters]', - ] - ) - ->addModelTransformer(new ParametersTransformer()) - ->setAutoInitialize(false) - ->getForm(); - $fieldDefinitionForm ->add('QueryType', Type\ChoiceType::class, [ @@ -58,6 +45,7 @@ public function mapFieldDefinitionForm(FormInterface $fieldDefinitionForm, Field 'required' => true, ] ) + ->add('SetQueryType', Type\SubmitType::class, ['label' => 'Set']) ->add('ReturnedType', Type\ChoiceType::class, [ 'label' => 'Returned type', @@ -66,14 +54,18 @@ public function mapFieldDefinitionForm(FormInterface $fieldDefinitionForm, Field 'required' => true, ] ) - ->add($parametersForm); + ->add('Parameters', FieldDefinitionParametersType::class, + [ + 'property_path' => 'fieldSettings[Parameters]', + 'query_type' => $data->fieldSettings['QueryType'], + ] + ); } public function mapFieldValueForm(FormInterface $fieldForm, FieldData $data) { $fieldDefinition = $data->fieldDefinition; $formConfig = $fieldForm->getConfig(); - $validatorConfiguration = $fieldDefinition->getValidatorConfiguration(); $names = $fieldDefinition->getNames(); $label = $fieldDefinition->getName($formConfig->getOption('mainLanguageCode')) ?: reset($names);