Skip to content

Commit 876f528

Browse files
author
Bertrand Dunogier
committed
Named query field parameters form widgets
1 parent 958be49 commit 876f528

File tree

4 files changed

+110
-14
lines changed

4 files changed

+110
-14
lines changed

src/Symfony/Resources/config/services/ezplatform.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,13 @@ services:
4646
decorates: ezpublish.templating.field_block_renderer.twig
4747
arguments:
4848
$innerRenderer: '@EzSystems\EzPlatformQueryFieldType\eZ\Twig\QueryFieldBlockRenderer.inner'
49+
50+
EzSystems\EzPlatformQueryFieldType\eZ\FieldType\NamedQuery\Form\FieldDefinitionParametersType:
51+
arguments:
52+
$parametersSubscriber: '@EzSystems\EzPlatformQueryFieldType\eZ\FieldType\NamedQuery\Form\EventSubscriber\FieldDefinitionParametersSubscriber'
53+
tags:
54+
- { name: form.type }
55+
56+
EzSystems\EzPlatformQueryFieldType\eZ\FieldType\NamedQuery\Form\EventSubscriber\FieldDefinitionParametersSubscriber:
57+
tags:
58+
- { name: kernel.event_subscriber }
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
/**
4+
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
5+
* @license For full copyright and license information view LICENSE file distributed with this source code.
6+
*/
7+
namespace EzSystems\EzPlatformQueryFieldType\eZ\FieldType\NamedQuery\Form\EventSubscriber;
8+
9+
use eZ\Publish\Core\QueryType\QueryTypeRegistry;
10+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
11+
use Symfony\Component\Form\Extension\Core\Type;
12+
use Symfony\Component\Form\FormEvent;
13+
use Symfony\Component\Form\FormEvents;
14+
15+
class FieldDefinitionParametersSubscriber implements EventSubscriberInterface
16+
{
17+
/** @var \eZ\Publish\Core\QueryType\QueryTypeRegistry */
18+
private $queryTypeRegistry;
19+
20+
public function __construct(QueryTypeRegistry $queryTypeRegistry)
21+
{
22+
$this->queryTypeRegistry = $queryTypeRegistry;
23+
}
24+
25+
public static function getSubscribedEvents()
26+
{
27+
return [FormEvents::PRE_SET_DATA => 'addParametersFormFields'];
28+
}
29+
30+
public function addParametersFormFields(FormEvent $event)
31+
{
32+
$data = $event->getData();
33+
if ($data === null) {
34+
return;
35+
}
36+
37+
$queryTypeIdentifier = $event->getForm()->getConfig()->getOption('query_type');
38+
if ($queryTypeIdentifier === null) {
39+
return;
40+
}
41+
42+
$queryType = $this->queryTypeRegistry->getQueryType($queryTypeIdentifier);
43+
foreach ($queryType->getSupportedParameters() as $parameter) {
44+
$event->getForm()->add(
45+
$parameter,
46+
Type\TextType::class,
47+
[
48+
'label' => $parameter,
49+
'property_path' => sprintf('[Parameters][%s]', $parameter),
50+
'required' => false,
51+
]
52+
);
53+
}
54+
}
55+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
/**
4+
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
5+
* @license For full copyright and license information view LICENSE file distributed with this source code.
6+
*/
7+
namespace EzSystems\EzPlatformQueryFieldType\eZ\FieldType\NamedQuery\Form;
8+
9+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
10+
use Symfony\Component\Form\AbstractType;
11+
use Symfony\Component\Form\Extension\Core\Type;
12+
use Symfony\Component\Form\FormBuilderInterface;
13+
use Symfony\Component\OptionsResolver\OptionsResolver;
14+
15+
class FieldDefinitionParametersType extends AbstractType
16+
{
17+
/** @var \Symfony\Component\EventDispatcher\EventSubscriberInterface */
18+
private $parametersSubscriber;
19+
20+
public function __construct(EventSubscriberInterface $parametersSubscriber)
21+
{
22+
$this->parametersSubscriber = $parametersSubscriber;
23+
}
24+
25+
public function getParent()
26+
{
27+
return Type\FormType::class;
28+
}
29+
30+
public function configureOptions(OptionsResolver $resolver)
31+
{
32+
$resolver->setDefault('query_type', null);
33+
}
34+
35+
public function buildForm(FormBuilderInterface $builder, array $options)
36+
{
37+
$builder->addEventSubscriber($this->parametersSubscriber);
38+
}
39+
}

src/eZ/FieldType/NamedQuery/Mapper.php

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use eZ\Publish\API\Repository\ContentTypeService;
1010
use eZ\Publish\Core\QueryType\QueryTypeRegistry;
1111
use EzSystems\EzPlatformQueryFieldType\eZ\FieldType\Mapper\ParametersTransformer;
12+
use EzSystems\EzPlatformQueryFieldType\eZ\FieldType\NamedQuery\Form\FieldDefinitionParametersType;
1213
use EzSystems\RepositoryForms\Data\FieldDefinitionData;
1314
use EzSystems\RepositoryForms\FieldType\FieldDefinitionFormMapperInterface;
1415
use Symfony\Component\Form\Extension\Core\Type;
@@ -37,19 +38,6 @@ public function __construct(ContentTypeService $contentTypeService, QueryTypeReg
3738

3839
public function mapFieldDefinitionForm(FormInterface $fieldDefinitionForm, FieldDefinitionData $data)
3940
{
40-
$parametersForm = $fieldDefinitionForm->getConfig()->getFormFactory()->createBuilder()
41-
->create(
42-
'Parameters',
43-
Type\TextareaType::class,
44-
[
45-
'label' => 'Parameters',
46-
'property_path' => 'fieldSettings[Parameters]',
47-
]
48-
)
49-
->addModelTransformer(new ParametersTransformer())
50-
->setAutoInitialize(false)
51-
->getForm();
52-
5341
$fieldDefinitionForm
5442
->add('ReturnedType', Type\ChoiceType::class,
5543
[
@@ -59,7 +47,11 @@ public function mapFieldDefinitionForm(FormInterface $fieldDefinitionForm, Field
5947
'required' => true,
6048
]
6149
)
62-
->add($parametersForm);
50+
->add('Parameters', FieldDefinitionParametersType::class,
51+
[
52+
'property_path' => 'fieldSettings[Parameters]',
53+
'query_type' => $data->fieldSettings['QueryType'],
54+
]);
6355
}
6456

6557
public function configureOptions(OptionsResolver $resolver)

0 commit comments

Comments
 (0)