Skip to content

Commit

Permalink
Prototyped named types
Browse files Browse the repository at this point in the history
  • Loading branch information
Bertrand Dunogier committed Feb 18, 2020
1 parent e30726e commit e28f815
Show file tree
Hide file tree
Showing 10 changed files with 594 additions and 1 deletion.
2 changes: 1 addition & 1 deletion doc/research/named_field_types.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ ezplatform:
children:
query_type: eZ:Children
default_parameters:
location: '@=location'
location: '@=mainLocation'
type: '@=returnedType'
relating_content:
query_type: eZ:ContentRelatedTo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@
*/
namespace EzSystems\EzPlatformQueryFieldType\Symfony\DependencyInjection;

use EzSystems\EzPlatformQueryFieldType\eZ\FieldType\NamedQuery;
use EzSystems\EzPlatformQueryFieldType\eZ\Persistence\Legacy\Content\FieldValue\Converter\QueryConverter;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\Config\FileLocator;
Expand All @@ -27,6 +31,7 @@ public function load(array $configs, ContainerBuilder $container)
$loader->load('services.yml');

$this->addContentViewConfig($container);
$this->handleNamedTypes($container);
}

public function prepend(ContainerBuilder $container)
Expand Down Expand Up @@ -100,4 +105,51 @@ protected function prependFieldTemplateConfig(ContainerBuilder $container): void
$container->prependExtensionConfig('ezpublish', $config);
$container->addResource(new FileResource($configFile));
}

private function handleNamedTypes(ContainerBuilder $container)
{
if (!$container->hasParameter('ezcontentquery_named')) {
return;
}

foreach ($container->getParameter('ezcontentquery_named') as $name => $config) {
// @todo validate name syntax
$fieldTypeIdentifier = 'ezcontentquery_' . $name;

$this->defineFieldTypeService($container, $fieldTypeIdentifier, $config);
$this->tagFieldTypeConverter($container, $fieldTypeIdentifier);
$this->tagFieldTypeFormMapper($container, $config, $fieldTypeIdentifier);
}
}

private function defineFieldTypeService(ContainerBuilder $container, string $fieldTypeIdentifier, array $config)
{
$serviceId = NamedQuery\Type::class . '\\' . $fieldTypeIdentifier;

$definition = new ChildDefinition('ezpublish.fieldType');
$definition->setClass(NamedQuery\Type::class);
$definition->setAutowired(true);
$definition->setPublic(true);
$definition->addTag('ezpublish.fieldType', ['alias' => $fieldTypeIdentifier]);
$definition->setArgument('$identifier', $fieldTypeIdentifier);
$definition->setArgument('$config', $config);
$container->setDefinition($serviceId, $definition);
}

private function tagFieldTypeConverter(ContainerBuilder $container, string $fieldTypeIdentifier)
{
$container->getDefinition(QueryConverter::class)->addTag(
'ezpublish.storageEngine.legacy.converter',
['alias' => $fieldTypeIdentifier]
);
}

private function tagFieldTypeFormMapper(ContainerBuilder $container, array $config, string $fieldTypeIdentifier)
{
$definition = new Definition(NamedQuery\Mapper::class);
$definition->addTag('ez.fieldFormMapper.definition', ['fieldType' => $fieldTypeIdentifier]);
$definition->setAutowired(true);
$definition->setArgument('$queryType', $config['query_type']);
$container->setDefinition(NamedQuery\Mapper::class . '\\' . $fieldTypeIdentifier, $definition);
}
}
11 changes: 11 additions & 0 deletions src/Symfony/Resources/config/default_parameters.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,14 @@ parameters:
ezcontentquery_field_view: 'content_query_field'
ezcontentquery_item_view: 'line'
ezcontentquery_identifier: 'ezcontentquery'
ezcontentquery_named:
children:
query_type: eZ:Children
default_parameters:
location: 'mainLocation'
type: 'returnedType'
relating:
query_type: AppBundle:RelatedToContent
default_parameters:
to_content: 'content'
type: 'returnedType'
5 changes: 5 additions & 0 deletions src/Symfony/Resources/config/services/ezplatform.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,8 @@ services:
$views: { field: '%ezcontentquery_field_view%', item: '%ezcontentquery_item_view%' }
tags:
- { name: kernel.event_subscriber }

EzSystems\EzPlatformQueryFieldType\eZ\Twig\QueryFieldBlockRenderer:
decorates: ezpublish.templating.field_block_renderer.twig
arguments:
$innerRenderer: '@EzSystems\EzPlatformQueryFieldType\eZ\Twig\QueryFieldBlockRenderer.inner'
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,17 @@
{{- form_widget(form.Parameters) -}}
</div>
{% endblock %}

{% block ezcontentquery_named_field_definition_edit %}
<div class="query-returned-type{% if group_class is not empty %} {{ group_class }}{% endif %}">
{{- form_label(form.ReturnedType) -}}
{{- form_errors(form.ReturnedType) -}}
{{- form_widget(form.ReturnedType) -}}
</div>

<div class="query-parameters{% if group_class is not empty %} {{ group_class }}{% endif %}">
{{- form_label(form.Parameters) -}}
{{- form_errors(form.Parameters) -}}
{{- form_widget(form.Parameters) -}}
</div>
{% endblock %}
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@
{{ block( 'settings_defaultvalue' ) }}
</ul>
{% endblock %}

{% block ezcontentquery_named_settings %}
{% endblock %}
81 changes: 81 additions & 0 deletions src/eZ/FieldType/NamedQuery/Mapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?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;

use eZ\Publish\API\Repository\ContentTypeService;
use eZ\Publish\Core\QueryType\QueryTypeRegistry;
use EzSystems\EzPlatformQueryFieldType\eZ\FieldType\Mapper\ParametersTransformer;
use EzSystems\RepositoryForms\Data\FieldDefinitionData;
use EzSystems\RepositoryForms\FieldType\FieldDefinitionFormMapperInterface;
use Symfony\Component\Form\Extension\Core\Type;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

final class Mapper implements FieldDefinitionFormMapperInterface
{
/** @var ContentTypeService */
private $contentTypeService;
/**
* @var string
*/
private $queryType;
/**
* @var \eZ\Publish\Core\QueryType\QueryTypeRegistry
*/
private $queryTypeRegistry;

public function __construct(ContentTypeService $contentTypeService, QueryTypeRegistry $queryTypeRegistry, string $queryType)
{
$this->contentTypeService = $contentTypeService;
$this->queryType = $queryType;
$this->queryTypeRegistry = $queryTypeRegistry;
}

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,
[
'label' => 'Returned type',
'property_path' => 'fieldSettings[ReturnedType]',
'choices' => $this->getContentTypes(),
'required' => true,
]
)
->add($parametersForm);
}

public function configureOptions(OptionsResolver $resolver)
{
$resolver
->setDefaults([
'translation_domain' => 'ezrepoforms_content_type',
]);
}

private function getContentTypes()
{
foreach ($this->contentTypeService->loadContentTypeGroups() as $contentTypeGroup) {
foreach ($this->contentTypeService->loadContentTypes($contentTypeGroup) as $contentType) {
yield $contentType->getName() => $contentType->identifier;
}
}
}
}
Loading

0 comments on commit e28f815

Please sign in to comment.