Skip to content

Commit e28f815

Browse files
author
Bertrand Dunogier
committed
Prototyped named types
1 parent e30726e commit e28f815

File tree

10 files changed

+594
-1
lines changed

10 files changed

+594
-1
lines changed

doc/research/named_field_types.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ ezplatform:
1010
children:
1111
query_type: eZ:Children
1212
default_parameters:
13-
location: '@=location'
13+
location: '@=mainLocation'
1414
type: '@=returnedType'
1515
relating_content:
1616
query_type: eZ:ContentRelatedTo

src/Symfony/DependencyInjection/EzSystemsEzPlatformQueryFieldTypeExtension.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@
66
*/
77
namespace EzSystems\EzPlatformQueryFieldType\Symfony\DependencyInjection;
88

9+
use EzSystems\EzPlatformQueryFieldType\eZ\FieldType\NamedQuery;
10+
use EzSystems\EzPlatformQueryFieldType\eZ\Persistence\Legacy\Content\FieldValue\Converter\QueryConverter;
911
use Symfony\Component\Config\Resource\FileResource;
12+
use Symfony\Component\DependencyInjection\ChildDefinition;
1013
use Symfony\Component\DependencyInjection\ContainerBuilder;
14+
use Symfony\Component\DependencyInjection\Definition;
1115
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
1216
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
1317
use Symfony\Component\Config\FileLocator;
@@ -27,6 +31,7 @@ public function load(array $configs, ContainerBuilder $container)
2731
$loader->load('services.yml');
2832

2933
$this->addContentViewConfig($container);
34+
$this->handleNamedTypes($container);
3035
}
3136

3237
public function prepend(ContainerBuilder $container)
@@ -100,4 +105,51 @@ protected function prependFieldTemplateConfig(ContainerBuilder $container): void
100105
$container->prependExtensionConfig('ezpublish', $config);
101106
$container->addResource(new FileResource($configFile));
102107
}
108+
109+
private function handleNamedTypes(ContainerBuilder $container)
110+
{
111+
if (!$container->hasParameter('ezcontentquery_named')) {
112+
return;
113+
}
114+
115+
foreach ($container->getParameter('ezcontentquery_named') as $name => $config) {
116+
// @todo validate name syntax
117+
$fieldTypeIdentifier = 'ezcontentquery_' . $name;
118+
119+
$this->defineFieldTypeService($container, $fieldTypeIdentifier, $config);
120+
$this->tagFieldTypeConverter($container, $fieldTypeIdentifier);
121+
$this->tagFieldTypeFormMapper($container, $config, $fieldTypeIdentifier);
122+
}
123+
}
124+
125+
private function defineFieldTypeService(ContainerBuilder $container, string $fieldTypeIdentifier, array $config)
126+
{
127+
$serviceId = NamedQuery\Type::class . '\\' . $fieldTypeIdentifier;
128+
129+
$definition = new ChildDefinition('ezpublish.fieldType');
130+
$definition->setClass(NamedQuery\Type::class);
131+
$definition->setAutowired(true);
132+
$definition->setPublic(true);
133+
$definition->addTag('ezpublish.fieldType', ['alias' => $fieldTypeIdentifier]);
134+
$definition->setArgument('$identifier', $fieldTypeIdentifier);
135+
$definition->setArgument('$config', $config);
136+
$container->setDefinition($serviceId, $definition);
137+
}
138+
139+
private function tagFieldTypeConverter(ContainerBuilder $container, string $fieldTypeIdentifier)
140+
{
141+
$container->getDefinition(QueryConverter::class)->addTag(
142+
'ezpublish.storageEngine.legacy.converter',
143+
['alias' => $fieldTypeIdentifier]
144+
);
145+
}
146+
147+
private function tagFieldTypeFormMapper(ContainerBuilder $container, array $config, string $fieldTypeIdentifier)
148+
{
149+
$definition = new Definition(NamedQuery\Mapper::class);
150+
$definition->addTag('ez.fieldFormMapper.definition', ['fieldType' => $fieldTypeIdentifier]);
151+
$definition->setAutowired(true);
152+
$definition->setArgument('$queryType', $config['query_type']);
153+
$container->setDefinition(NamedQuery\Mapper::class . '\\' . $fieldTypeIdentifier, $definition);
154+
}
103155
}

src/Symfony/Resources/config/default_parameters.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,14 @@ parameters:
22
ezcontentquery_field_view: 'content_query_field'
33
ezcontentquery_item_view: 'line'
44
ezcontentquery_identifier: 'ezcontentquery'
5+
ezcontentquery_named:
6+
children:
7+
query_type: eZ:Children
8+
default_parameters:
9+
location: 'mainLocation'
10+
type: 'returnedType'
11+
relating:
12+
query_type: AppBundle:RelatedToContent
13+
default_parameters:
14+
to_content: 'content'
15+
type: 'returnedType'

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,8 @@ services:
4343
$views: { field: '%ezcontentquery_field_view%', item: '%ezcontentquery_item_view%' }
4444
tags:
4545
- { name: kernel.event_subscriber }
46+
47+
EzSystems\EzPlatformQueryFieldType\eZ\Twig\QueryFieldBlockRenderer:
48+
decorates: ezpublish.templating.field_block_renderer.twig
49+
arguments:
50+
$innerRenderer: '@EzSystems\EzPlatformQueryFieldType\eZ\Twig\QueryFieldBlockRenderer.inner'

src/Symfony/Resources/views/fieldtype/fielddefinition_edit.html.twig

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,17 @@
3131
{{- form_widget(form.Parameters) -}}
3232
</div>
3333
{% endblock %}
34+
35+
{% block ezcontentquery_named_field_definition_edit %}
36+
<div class="query-returned-type{% if group_class is not empty %} {{ group_class }}{% endif %}">
37+
{{- form_label(form.ReturnedType) -}}
38+
{{- form_errors(form.ReturnedType) -}}
39+
{{- form_widget(form.ReturnedType) -}}
40+
</div>
41+
42+
<div class="query-parameters{% if group_class is not empty %} {{ group_class }}{% endif %}">
43+
{{- form_label(form.Parameters) -}}
44+
{{- form_errors(form.Parameters) -}}
45+
{{- form_widget(form.Parameters) -}}
46+
</div>
47+
{% endblock %}

src/Symfony/Resources/views/fieldtype/fielddefinition_settings.html.html.twig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,6 @@
2121
{{ block( 'settings_defaultvalue' ) }}
2222
</ul>
2323
{% endblock %}
24+
25+
{% block ezcontentquery_named_settings %}
26+
{% endblock %}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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;
8+
9+
use eZ\Publish\API\Repository\ContentTypeService;
10+
use eZ\Publish\Core\QueryType\QueryTypeRegistry;
11+
use EzSystems\EzPlatformQueryFieldType\eZ\FieldType\Mapper\ParametersTransformer;
12+
use EzSystems\RepositoryForms\Data\FieldDefinitionData;
13+
use EzSystems\RepositoryForms\FieldType\FieldDefinitionFormMapperInterface;
14+
use Symfony\Component\Form\Extension\Core\Type;
15+
use Symfony\Component\Form\FormInterface;
16+
use Symfony\Component\OptionsResolver\OptionsResolver;
17+
18+
final class Mapper implements FieldDefinitionFormMapperInterface
19+
{
20+
/** @var ContentTypeService */
21+
private $contentTypeService;
22+
/**
23+
* @var string
24+
*/
25+
private $queryType;
26+
/**
27+
* @var \eZ\Publish\Core\QueryType\QueryTypeRegistry
28+
*/
29+
private $queryTypeRegistry;
30+
31+
public function __construct(ContentTypeService $contentTypeService, QueryTypeRegistry $queryTypeRegistry, string $queryType)
32+
{
33+
$this->contentTypeService = $contentTypeService;
34+
$this->queryType = $queryType;
35+
$this->queryTypeRegistry = $queryTypeRegistry;
36+
}
37+
38+
public function mapFieldDefinitionForm(FormInterface $fieldDefinitionForm, FieldDefinitionData $data)
39+
{
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+
53+
$fieldDefinitionForm
54+
->add('ReturnedType', Type\ChoiceType::class,
55+
[
56+
'label' => 'Returned type',
57+
'property_path' => 'fieldSettings[ReturnedType]',
58+
'choices' => $this->getContentTypes(),
59+
'required' => true,
60+
]
61+
)
62+
->add($parametersForm);
63+
}
64+
65+
public function configureOptions(OptionsResolver $resolver)
66+
{
67+
$resolver
68+
->setDefaults([
69+
'translation_domain' => 'ezrepoforms_content_type',
70+
]);
71+
}
72+
73+
private function getContentTypes()
74+
{
75+
foreach ($this->contentTypeService->loadContentTypeGroups() as $contentTypeGroup) {
76+
foreach ($this->contentTypeService->loadContentTypes($contentTypeGroup) as $contentType) {
77+
yield $contentType->getName() => $contentType->identifier;
78+
}
79+
}
80+
}
81+
}

0 commit comments

Comments
 (0)