Skip to content

Commit

Permalink
Prototyped a more advanced query type parameters UI
Browse files Browse the repository at this point in the history
  • Loading branch information
Bertrand Dunogier committed Dec 13, 2019
1 parent 4980d89 commit f2e208d
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 46 deletions.
10 changes: 10 additions & 0 deletions src/Symfony/Resources/config/services/ezplatform.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
6 changes: 6 additions & 0 deletions src/Symfony/Resources/views/field_types.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
{{- form_label(form.QueryType) -}}
{{- form_errors(form.QueryType) -}}
{{- form_widget(form.QueryType) -}}

{{- form_widget(form.SetQueryType) -}}
</div>

<div class="query-returned-type{% if group_class is not empty %} {{ group_class }}{% endif %}">
Expand All @@ -14,8 +16,12 @@
</div>

<div class="query-parameters{% if group_class is not empty %} {{ group_class }}{% endif %}">
{% 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 %}
</div>
{% endblock %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace EzSystems\EzPlatformQueryFieldType\eZ\FieldType\Form\EventSubscriber;

use eZ\Publish\Core\QueryType\QueryTypeRegistry;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\Extension\Core\Type;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;

class FieldDefinitionParametersSubscriber implements EventSubscriberInterface
{
/** @var \eZ\Publish\Core\QueryType\QueryTypeRegistry */
private $queryTypeRegistry;

public function __construct(QueryTypeRegistry $queryTypeRegistry)
{
$this->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,
]
);
}
}
}
39 changes: 39 additions & 0 deletions src/eZ/FieldType/Form/FieldDefinitionParametersType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace EzSystems\EzPlatformQueryFieldType\eZ\FieldType\Form;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class FieldDefinitionParametersType extends AbstractType
{
/** @var \Symfony\Component\EventDispatcher\EventSubscriberInterface */
private $parametersSubscriber;

public function __construct(EventSubscriberInterface $parametersSubscriber)
{
$this->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);
}
}
31 changes: 0 additions & 31 deletions src/eZ/FieldType/Mapper/ParametersTransformer.php

This file was deleted.

22 changes: 7 additions & 15 deletions src/eZ/FieldType/Mapper/QueryFormMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,6 @@ public function __construct(ContentTypeService $contentTypeService, array $query

public function mapFieldDefinitionForm(FormInterface $fieldDefinitionForm, FieldDefinitionData $data)
{
$parametersForm = $fieldDefinitionForm->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,
[
Expand All @@ -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',
Expand All @@ -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);

Expand Down

0 comments on commit f2e208d

Please sign in to comment.