From e30726ee6bbaf5646b426d71a7115a04566546d5 Mon Sep 17 00:00:00 2001 From: Bertrand Dunogier Date: Sat, 21 Dec 2019 02:59:40 +0100 Subject: [PATCH 1/4] Added research doc for named types --- doc/research/named_field_types.md | 42 +++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 doc/research/named_field_types.md diff --git a/doc/research/named_field_types.md b/doc/research/named_field_types.md new file mode 100644 index 0000000..73f0113 --- /dev/null +++ b/doc/research/named_field_types.md @@ -0,0 +1,42 @@ +# Named query field types + +A higher level version of the query field type. Through configuration, queries are associated to a name. Those are added added to the list of available field types. When added, they query type isn't show, and the parameters are immediately displayed for editing. It saves time when modelling the content by allowing to reuse the same type for a similar concept. + +## Examples + +``` +ezplatform: + named_query_types: + children: + query_type: eZ:Children + default_parameters: + location: '@=location' + type: '@=returnedType' + relating_content: + query_type: eZ:ContentRelatedTo + default_parameters: + to_content: '@=content' + type: '@=returnedType' +``` + +## Extra features + +### Default query type parameters + +Content and location level (not field) based parameters can get a default value: the current content, its section, the returned type... + +### Translation + +That extra layer is a good place for translating parameters. + +### Customization + +Custom templates could be associated to named query field types, giving extra flexibility. + +### Extensibiliy + +Named queries make it easy for 3rd parties to add their own field types without developing any: + +- define new query types, with custom criteria if needed +- define named queries + From e28f81558afdcf8b865e08b33c46954a608080f3 Mon Sep 17 00:00:00 2001 From: Bertrand Dunogier Date: Sat, 21 Dec 2019 13:34:31 +0100 Subject: [PATCH 2/4] Prototyped named types --- doc/research/named_field_types.md | 2 +- ...stemsEzPlatformQueryFieldTypeExtension.php | 52 ++++ .../Resources/config/default_parameters.yml | 11 + .../Resources/config/services/ezplatform.yml | 5 + .../fieldtype/fielddefinition_edit.html.twig | 14 + .../fielddefinition_settings.html.html.twig | 3 + src/eZ/FieldType/NamedQuery/Mapper.php | 81 ++++++ src/eZ/FieldType/NamedQuery/Type.php | 244 ++++++++++++++++++ src/eZ/FieldType/NamedQuery/Value.php | 37 +++ src/eZ/Twig/QueryFieldBlockRenderer.php | 146 +++++++++++ 10 files changed, 594 insertions(+), 1 deletion(-) create mode 100644 src/eZ/FieldType/NamedQuery/Mapper.php create mode 100644 src/eZ/FieldType/NamedQuery/Type.php create mode 100644 src/eZ/FieldType/NamedQuery/Value.php create mode 100644 src/eZ/Twig/QueryFieldBlockRenderer.php diff --git a/doc/research/named_field_types.md b/doc/research/named_field_types.md index 73f0113..319c1d6 100644 --- a/doc/research/named_field_types.md +++ b/doc/research/named_field_types.md @@ -10,7 +10,7 @@ ezplatform: children: query_type: eZ:Children default_parameters: - location: '@=location' + location: '@=mainLocation' type: '@=returnedType' relating_content: query_type: eZ:ContentRelatedTo diff --git a/src/Symfony/DependencyInjection/EzSystemsEzPlatformQueryFieldTypeExtension.php b/src/Symfony/DependencyInjection/EzSystemsEzPlatformQueryFieldTypeExtension.php index cd15c02..3bad963 100644 --- a/src/Symfony/DependencyInjection/EzSystemsEzPlatformQueryFieldTypeExtension.php +++ b/src/Symfony/DependencyInjection/EzSystemsEzPlatformQueryFieldTypeExtension.php @@ -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; @@ -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) @@ -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); + } } diff --git a/src/Symfony/Resources/config/default_parameters.yml b/src/Symfony/Resources/config/default_parameters.yml index 66f8f5c..a37d006 100644 --- a/src/Symfony/Resources/config/default_parameters.yml +++ b/src/Symfony/Resources/config/default_parameters.yml @@ -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' diff --git a/src/Symfony/Resources/config/services/ezplatform.yml b/src/Symfony/Resources/config/services/ezplatform.yml index c8df223..a485291 100644 --- a/src/Symfony/Resources/config/services/ezplatform.yml +++ b/src/Symfony/Resources/config/services/ezplatform.yml @@ -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' diff --git a/src/Symfony/Resources/views/fieldtype/fielddefinition_edit.html.twig b/src/Symfony/Resources/views/fieldtype/fielddefinition_edit.html.twig index 4f357a2..7ec8ac3 100644 --- a/src/Symfony/Resources/views/fieldtype/fielddefinition_edit.html.twig +++ b/src/Symfony/Resources/views/fieldtype/fielddefinition_edit.html.twig @@ -31,3 +31,17 @@ {{- form_widget(form.Parameters) -}} {% endblock %} + +{% block ezcontentquery_named_field_definition_edit %} +
+ {{- form_label(form.ReturnedType) -}} + {{- form_errors(form.ReturnedType) -}} + {{- form_widget(form.ReturnedType) -}} +
+ +
+ {{- form_label(form.Parameters) -}} + {{- form_errors(form.Parameters) -}} + {{- form_widget(form.Parameters) -}} +
+{% endblock %} diff --git a/src/Symfony/Resources/views/fieldtype/fielddefinition_settings.html.html.twig b/src/Symfony/Resources/views/fieldtype/fielddefinition_settings.html.html.twig index 7a867f4..d8680ff 100644 --- a/src/Symfony/Resources/views/fieldtype/fielddefinition_settings.html.html.twig +++ b/src/Symfony/Resources/views/fieldtype/fielddefinition_settings.html.html.twig @@ -21,3 +21,6 @@ {{ block( 'settings_defaultvalue' ) }} {% endblock %} + +{% block ezcontentquery_named_settings %} +{% endblock %} diff --git a/src/eZ/FieldType/NamedQuery/Mapper.php b/src/eZ/FieldType/NamedQuery/Mapper.php new file mode 100644 index 0000000..3b5441c --- /dev/null +++ b/src/eZ/FieldType/NamedQuery/Mapper.php @@ -0,0 +1,81 @@ +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; + } + } + } +} diff --git a/src/eZ/FieldType/NamedQuery/Type.php b/src/eZ/FieldType/NamedQuery/Type.php new file mode 100644 index 0000000..2d9df76 --- /dev/null +++ b/src/eZ/FieldType/NamedQuery/Type.php @@ -0,0 +1,244 @@ + ['type' => 'string', 'default' => null], + 'Parameters' => ['type' => 'array', 'default' => []], + 'ReturnedType' => ['type' => 'string', 'default' => ''], + ]; + + /** @var \eZ\Publish\Core\QueryType\QueryTypeRegistry */ + private $queryTypeRegistry; + + /** @var \eZ\Publish\API\Repository\ContentTypeService */ + private $contentTypeService; + + /** @var string */ + private $identifier; + + /** @var array */ + private $config; + + public function __construct( + QueryTypeRegistry $queryTypeRegistry, + ContentTypeService $contentTypeService, + string $identifier, + array $config + ) { + $this->queryTypeRegistry = $queryTypeRegistry; + $this->contentTypeService = $contentTypeService; + $this->identifier = $identifier; + // @todo error handling on $config + $this->config = $config; + } + + public function applyDefaultSettings(&$fieldSettings) + { + parent::applyDefaultSettings($fieldSettings); + $fieldSettings['QueryType'] = $this->config['query_type']; + foreach ($this->config['default_parameters'] as $parameterName => $parameterValue) { + $fieldSettings['Parameters'][$parameterName] = '@=' . $parameterValue; + } + } + + /** + * Validates the validatorConfiguration of a FieldDefinitionCreateStruct or FieldDefinitionUpdateStruct. + * + * @param mixed $validatorConfiguration + * + * @return \eZ\Publish\SPI\FieldType\ValidationError[] + */ + public function validateValidatorConfiguration($validatorConfiguration) + { + $validationErrors = []; + + return $validationErrors; + } + + /** + * Validates a field based on the validators in the field definition. + * + * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException + * + * @param \eZ\Publish\API\Repository\Values\ContentType\FieldDefinition $fieldDefinition The field definition of the field + * @param \eZ\Publish\Core\FieldType\TextLine\Value $fieldValue The field value for which an action is performed + * + * @return \eZ\Publish\SPI\FieldType\ValidationError[] + */ + public function validate(FieldDefinition $fieldDefinition, SPIValue $fieldValue) + { + return []; + } + + /** + * Returns the field type identifier for this field type. + * + * @return string + */ + public function getFieldTypeIdentifier() + { + return $this->identifier; + } + + /** + * Returns the name of the given field value. + * + * It will be used to generate content name and url alias if current field is designated + * to be used in the content name/urlAlias pattern. + * + * @param \EzSystems\EzPlatformQueryFieldType\eZ\FieldType\Query\Value $value + * + * @return string + */ + public function getName(SPIValue $value) + { + return (string)$value->text; + } + + public function getEmptyValue() + { + return new Value(); + } + + /** + * Returns if the given $value is considered empty by the field type. + * + * @param mixed $value + * + * @return bool + */ + public function isEmptyValue(SPIValue $value) + { + return false; + } + + protected function createValueFromInput($inputValue) + { + if (is_string($inputValue)) { + $inputValue = new Value($inputValue); + } + + return $inputValue; + } + + /** + * Throws an exception if value structure is not of expected format. + * + * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the value does not match the expected structure + * + * @param \eZ\Publish\Core\FieldType\TextLine\Value $value + */ + protected function checkValueStructure(BaseValue $value) + { + if (!is_string($value->text)) { + throw new InvalidArgumentType( + '$value->text', + 'string', + $value->text + ); + } + } + + /** + * Returns information for FieldValue->$sortKey relevant to the field type. + * + * @param \eZ\Publish\Core\FieldType\TextLine\Value $value + * + * @return array + */ + protected function getSortInfo(BaseValue $value) + { + return $this->transformationProcessor->transformByGroup((string)$value, 'lowercase'); + } + + /** + * Converts an $hash to the Value defined by the field type. + * + * @param mixed $hash + * + * @return \eZ\Publish\Core\FieldType\TextLine\Value $value + */ + public function fromHash($hash) + { + if ($hash === null) { + return $this->getEmptyValue(); + } + + return new Value($hash); + } + + /** + * Converts a $Value to a hash. + * + * @param \eZ\Publish\Core\FieldType\TextLine\Value $value + * + * @return mixed + */ + public function toHash(SPIValue $value) + { + if ($this->isEmptyValue($value)) { + return null; + } + + return $value->text; + } + + /** + * Returns whether the field type is searchable. + * + * @return bool + */ + public function isSearchable() + { + return false; + } + + public function validateFieldSettings($fieldSettings) + { + $errors = []; + + if (isset($fieldSettings['QueryType']) && $fieldSettings['QueryType'] !== '') { + try { + $this->queryTypeRegistry->getQueryType($fieldSettings['QueryType']); + } catch (InvalidArgumentException $e) { + $errors[] = new ValidationError('The selected query type does not exist'); + } + } + + if (isset($fieldSettings['ReturnedType']) && $fieldSettings['ReturnedType'] !== '') { + try { + $this->contentTypeService->loadContentTypeByIdentifier($fieldSettings['ReturnedType']); + } catch (NotFoundException $e) { + $errors[] = new ValidationError('The selected returned type could not be loaded'); + } + } + + if (isset($fieldSettings['Parameters'])) { + if (!is_array($fieldSettings['Parameters'])) { + $errors[] = new ValidationError('Parameters is not a valid YAML string'); + } + } + + return $errors; + } +} diff --git a/src/eZ/FieldType/NamedQuery/Value.php b/src/eZ/FieldType/NamedQuery/Value.php new file mode 100644 index 0000000..3c4f7f0 --- /dev/null +++ b/src/eZ/FieldType/NamedQuery/Value.php @@ -0,0 +1,37 @@ +text = $text; + } + + /** + * @see \eZ\Publish\Core\FieldType\Value + */ + public function __toString() + { + return (string)$this->text; + } +} diff --git a/src/eZ/Twig/QueryFieldBlockRenderer.php b/src/eZ/Twig/QueryFieldBlockRenderer.php new file mode 100644 index 0000000..b223506 --- /dev/null +++ b/src/eZ/Twig/QueryFieldBlockRenderer.php @@ -0,0 +1,146 @@ +innerRenderer = $innerRenderer; + } + + /** + * @inheritDoc + */ + public function renderContentFieldView(Field $field, $fieldTypeIdentifier, array $params = []) + { + if ($this->isNamedQueryField($fieldTypeIdentifier)) { + $fieldTypeIdentifier = 'ezcontentquery'; + } + + return $this->innerRenderer->renderContentFieldView($field, $fieldTypeIdentifier, $params); + } + + /** + * @inheritDoc + */ + public function renderContentFieldEdit(Field $field, $fieldTypeIdentifier, array $params = []) + { + if ($this->isNamedQueryField($fieldTypeIdentifier)) { + $fieldTypeIdentifier = 'ezcontentquery'; + } + + return $this->innerRenderer->renderContentFieldEdit($field, $fieldTypeIdentifier, $params); + } + + /** + * @inheritDoc + */ + public function renderFieldDefinitionView(FieldDefinition $fieldDefinition, array $params = []) + { + if ($this->isNamedQueryField($fieldDefinition->fieldTypeIdentifier)) { + $fieldDefinition = $this->overrideFieldDefinition($fieldDefinition); + } + + return $this->innerRenderer->renderFieldDefinitionEdit($fieldDefinition, $params); + } + + /** + * @inheritDoc + */ + public function renderFieldDefinitionEdit(FieldDefinition $fieldDefinition, array $params = []) + { + if ($this->isNamedQueryField($fieldDefinition->fieldTypeIdentifier)) { + $fieldDefinition = $this->overrideFieldDefinition($fieldDefinition); + } + + return $this->innerRenderer->renderFieldDefinitionEdit($fieldDefinition, $params); + } + + private function isNamedQueryField($identifier): bool + { + return strpos($identifier, 'ezcontentquery_') === 0; + } + + /** + * @param Twig_Environment $twig + */ + public function setTwig(Twig_Environment $twig) + { + $this->innerRenderer->setTwig($twig); + } + + /** + * @param string|Twig_Template $baseTemplate + */ + public function setBaseTemplate($baseTemplate) + { + $this->innerRenderer->setBaseTemplate($baseTemplate); + } + + /** + * @param array $fieldViewResources + */ + public function setFieldViewResources(array $fieldViewResources = null) + { + $this->innerRenderer->setFieldViewResources($fieldViewResources); + } + + /** + * @param array $fieldEditResources + */ + public function setFieldEditResources(array $fieldEditResources = null) + { + $this->innerRenderer->setFieldEditResources($fieldEditResources); + } + + /** + * @param array $fieldDefinitionViewResources + */ + public function setFieldDefinitionViewResources(array $fieldDefinitionViewResources = null) + { + $this->innerRenderer->setFieldDefinitionViewResources($fieldDefinitionViewResources); + } + + /** + * @param array $fieldDefinitionEditResources + */ + public function setFieldDefinitionEditResources(array $fieldDefinitionEditResources = null) + { + $this->innerRenderer->setFieldDefinitionEditResources($fieldDefinitionEditResources); + } + + /** + * @param \eZ\Publish\API\Repository\Values\ContentType\FieldDefinition $fieldDefinition + * + * @return \eZ\Publish\Core\Repository\Values\ContentType\FieldDefinition + */ + protected function overrideFieldDefinition(FieldDefinition $fieldDefinition): FieldDefinition + { + $properties = []; + foreach ($fieldDefinition->attributes() as $property) { + $properties[$property] = $fieldDefinition->$property; + } + $properties['fieldTypeIdentifier'] = 'ezcontentquery_named'; + + return new CoreFieldDefinition($properties); + } +} From c7efa3f2ff95ab18a753146d180f424a616dbfc5 Mon Sep 17 00:00:00 2001 From: Bertrand Dunogier Date: Fri, 13 Dec 2019 17:50:46 +0100 Subject: [PATCH 3/4] Named query field parameters form widgets --- .../Resources/config/services/ezplatform.yml | 10 ++++ .../FieldDefinitionParametersSubscriber.php | 55 +++++++++++++++++++ .../Form/FieldDefinitionParametersType.php | 39 +++++++++++++ src/eZ/FieldType/NamedQuery/Mapper.php | 20 ++----- 4 files changed, 110 insertions(+), 14 deletions(-) create mode 100644 src/eZ/FieldType/NamedQuery/Form/EventSubscriber/FieldDefinitionParametersSubscriber.php create mode 100644 src/eZ/FieldType/NamedQuery/Form/FieldDefinitionParametersType.php diff --git a/src/Symfony/Resources/config/services/ezplatform.yml b/src/Symfony/Resources/config/services/ezplatform.yml index a485291..1a0d9f2 100644 --- a/src/Symfony/Resources/config/services/ezplatform.yml +++ b/src/Symfony/Resources/config/services/ezplatform.yml @@ -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 } diff --git a/src/eZ/FieldType/NamedQuery/Form/EventSubscriber/FieldDefinitionParametersSubscriber.php b/src/eZ/FieldType/NamedQuery/Form/EventSubscriber/FieldDefinitionParametersSubscriber.php new file mode 100644 index 0000000..765cb66 --- /dev/null +++ b/src/eZ/FieldType/NamedQuery/Form/EventSubscriber/FieldDefinitionParametersSubscriber.php @@ -0,0 +1,55 @@ +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, + ] + ); + } + } +} diff --git a/src/eZ/FieldType/NamedQuery/Form/FieldDefinitionParametersType.php b/src/eZ/FieldType/NamedQuery/Form/FieldDefinitionParametersType.php new file mode 100644 index 0000000..abd241f --- /dev/null +++ b/src/eZ/FieldType/NamedQuery/Form/FieldDefinitionParametersType.php @@ -0,0 +1,39 @@ +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); + } +} diff --git a/src/eZ/FieldType/NamedQuery/Mapper.php b/src/eZ/FieldType/NamedQuery/Mapper.php index 3b5441c..14078b7 100644 --- a/src/eZ/FieldType/NamedQuery/Mapper.php +++ b/src/eZ/FieldType/NamedQuery/Mapper.php @@ -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; @@ -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, [ @@ -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) From f47851ba516dedc99c79926353d29eb60a0bc4f5 Mon Sep 17 00:00:00 2001 From: Bertrand Dunogier Date: Tue, 18 Feb 2020 01:48:09 +0100 Subject: [PATCH 4/4] More spec --- doc/research/named_field_types.md | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/doc/research/named_field_types.md b/doc/research/named_field_types.md index 319c1d6..d535042 100644 --- a/doc/research/named_field_types.md +++ b/doc/research/named_field_types.md @@ -6,14 +6,14 @@ A higher level version of the query field type. Through configuration, queries a ``` ezplatform: - named_query_types: + queries: children: - query_type: eZ:Children + type: eZ:Children default_parameters: location: '@=mainLocation' type: '@=returnedType' relating_content: - query_type: eZ:ContentRelatedTo + type: eZ:ContentRelatedTo default_parameters: to_content: '@=content' type: '@=returnedType' @@ -32,11 +32,12 @@ That extra layer is a good place for translating parameters. ### Customization Custom templates could be associated to named query field types, giving extra flexibility. +It would allow to use or extend the same template when the same list type is used, without +template configuration. ### Extensibiliy Named queries make it easy for 3rd parties to add their own field types without developing any: - define new query types, with custom criteria if needed -- define named queries - +- define named queries that would show up as field types, without implementing an actual field type