Skip to content

Commit 5b1f7ab

Browse files
author
Bertrand Dunogier
committed
Prototyped named types
1 parent bd986de commit 5b1f7ab

File tree

10 files changed

+595
-1
lines changed

10 files changed

+595
-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: 53 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,52 @@ 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+
// tag the query field type service the identifier
120+
$this->defineFieldTypeService($container, $fieldTypeIdentifier, $config);
121+
$this->tagFieldTypeConverter($container, $fieldTypeIdentifier);
122+
$this->tagFieldTypeFormMapper($container, $config, $fieldTypeIdentifier);
123+
}
124+
}
125+
126+
private function defineFieldTypeService(ContainerBuilder $container, string $fieldTypeIdentifier, array $config)
127+
{
128+
$serviceId = NamedQuery\Type::class . '\\' . $fieldTypeIdentifier;
129+
130+
$definition = new ChildDefinition('ezpublish.fieldType');
131+
$definition->setClass(NamedQuery\Type::class);
132+
$definition->setAutowired(true);
133+
$definition->setPublic(true);
134+
$definition->addTag('ezpublish.fieldType', ['alias' => $fieldTypeIdentifier]);
135+
$definition->setArgument('$identifier', $fieldTypeIdentifier);
136+
$definition->setArgument('$config', $config);
137+
$container->setDefinition($serviceId, $definition);
138+
}
139+
140+
private function tagFieldTypeConverter(ContainerBuilder $container, string $fieldTypeIdentifier)
141+
{
142+
$container->getDefinition(QueryConverter::class)->addTag(
143+
'ezpublish.storageEngine.legacy.converter',
144+
['alias' => $fieldTypeIdentifier]
145+
);
146+
}
147+
148+
private function tagFieldTypeFormMapper(ContainerBuilder $container, array $config, string $fieldTypeIdentifier)
149+
{
150+
$definition = new Definition(NamedQuery\Mapper::class);
151+
$definition->addTag('ez.fieldFormMapper.definition', ['fieldType' => $fieldTypeIdentifier]);
152+
$definition->setAutowired(true);
153+
$definition->setArgument('$queryType', $config['query_type']);
154+
$container->setDefinition(NamedQuery\Mapper::class . '\\' . $fieldTypeIdentifier, $definition);
155+
}
103156
}

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
@@ -41,3 +41,8 @@ services:
4141
$views: { field: '%ezcontentquery_field_view%', item: '%ezcontentquery_item_view%' }
4242
tags:
4343
- { name: kernel.event_subscriber }
44+
45+
EzSystems\EzPlatformQueryFieldType\eZ\Twig\QueryFieldBlockRenderer:
46+
decorates: ezpublish.templating.field_block_renderer.twig
47+
arguments:
48+
$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
@@ -19,3 +19,17 @@
1919
{{- form_widget(form.Parameters) -}}
2020
</div>
2121
{% endblock %}
22+
23+
{% block ezcontentquery_named_field_definition_edit %}
24+
<div class="query-returned-type{% if group_class is not empty %} {{ group_class }}{% endif %}">
25+
{{- form_label(form.ReturnedType) -}}
26+
{{- form_errors(form.ReturnedType) -}}
27+
{{- form_widget(form.ReturnedType) -}}
28+
</div>
29+
30+
<div class="query-parameters{% if group_class is not empty %} {{ group_class }}{% endif %}">
31+
{{- form_label(form.Parameters) -}}
32+
{{- form_errors(form.Parameters) -}}
33+
{{- form_widget(form.Parameters) -}}
34+
</div>
35+
{% 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)