Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 }
8 changes: 7 additions & 1 deletion src/Symfony/Resources/views/field_types.html.twig
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
{% extends '@EzSystemsRepositoryForms/ContentType/field_types.html.twig' %}

{% block ezcontentquery_field_definition_edit %}
<div class="query-query-type{% if group_class is not empty %} {{ group_class }}{% endif %}">
<div class="form-inline query-query-type{% if group_class is not empty %} {{ group_class }}{% endif %}">
{{- 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.

23 changes: 8 additions & 15 deletions src/eZ/FieldType/Mapper/QueryFormMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/
namespace EzSystems\EzPlatformQueryFieldType\eZ\FieldType\Mapper;

use EzSystems\EzPlatformQueryFieldType\eZ\FieldType\Form\FieldDefinitionParametersType;
use EzSystems\EzPlatformQueryFieldType\eZ\FieldType\Form\QueryFieldFormType;
use eZ\Publish\API\Repository\ContentTypeService;
use EzSystems\RepositoryForms\Data\Content\FieldData;
Expand Down Expand Up @@ -36,19 +37,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 +46,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 +55,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