Skip to content

Commit

Permalink
Named query field parameters form widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
Bertrand Dunogier committed Feb 18, 2020
1 parent e28f815 commit c7efa3f
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 14 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 @@ -48,3 +48,13 @@ services:
decorates: ezpublish.templating.field_block_renderer.twig
arguments:
$innerRenderer: '@EzSystems\EzPlatformQueryFieldType\eZ\Twig\QueryFieldBlockRenderer.inner'

EzSystems\EzPlatformQueryFieldType\eZ\FieldType\NamedQuery\Form\FieldDefinitionParametersType:
arguments:
$parametersSubscriber: '@EzSystems\EzPlatformQueryFieldType\eZ\FieldType\NamedQuery\Form\EventSubscriber\FieldDefinitionParametersSubscriber'
tags:
- { name: form.type }

EzSystems\EzPlatformQueryFieldType\eZ\FieldType\NamedQuery\Form\EventSubscriber\FieldDefinitionParametersSubscriber:
tags:
- { name: kernel.event_subscriber }
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\NamedQuery\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('[%s]', $parameter),
'required' => false,
]
);
}
}
}
39 changes: 39 additions & 0 deletions src/eZ/FieldType/NamedQuery/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\NamedQuery\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);
}
}
20 changes: 6 additions & 14 deletions src/eZ/FieldType/NamedQuery/Mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use eZ\Publish\API\Repository\ContentTypeService;
use eZ\Publish\Core\QueryType\QueryTypeRegistry;
use EzSystems\EzPlatformQueryFieldType\eZ\FieldType\Mapper\ParametersTransformer;
use EzSystems\EzPlatformQueryFieldType\eZ\FieldType\NamedQuery\Form\FieldDefinitionParametersType;
use EzSystems\RepositoryForms\Data\FieldDefinitionData;
use EzSystems\RepositoryForms\FieldType\FieldDefinitionFormMapperInterface;
use Symfony\Component\Form\Extension\Core\Type;
Expand Down Expand Up @@ -37,19 +38,6 @@ public function __construct(ContentTypeService $contentTypeService, QueryTypeReg

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('ReturnedType', Type\ChoiceType::class,
[
Expand All @@ -59,7 +47,11 @@ 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 configureOptions(OptionsResolver $resolver)
Expand Down

0 comments on commit c7efa3f

Please sign in to comment.