From 189e046fa6743dba0f25699ff9904659ca37793b Mon Sep 17 00:00:00 2001 From: Nattfarinn Date: Tue, 16 Apr 2024 09:16:19 +0200 Subject: [PATCH] fix: interface DefaultDataFieldStorage --- .../FieldType/DefaultDataFieldStorage.php | 26 +++++++++ .../Mapper/ResolveVirtualFieldSubscriber.php | 55 ++++++++++++++++++- 2 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 src/contracts/FieldType/DefaultDataFieldStorage.php diff --git a/src/contracts/FieldType/DefaultDataFieldStorage.php b/src/contracts/FieldType/DefaultDataFieldStorage.php new file mode 100644 index 0000000000..5ec92f761a --- /dev/null +++ b/src/contracts/FieldType/DefaultDataFieldStorage.php @@ -0,0 +1,26 @@ +value is a {@link \eZ\Publish\SPI\Persistence\Content\FieldValue} object. + * This value holds the data as a {@link \eZ\Publish\Core\FieldType\Value} based object, according to + * the field type (e.g. for TextLine, it will be a {@link \eZ\Publish\Core\FieldType\TextLine\Value} object). + * + * @param \eZ\Publish\SPI\Persistence\Content\VersionInfo $versionInfo + * @param \eZ\Publish\SPI\Persistence\Content\Field $field + */ + public function getDefaultFieldData(VersionInfo $versionInfo, Field $field): void; +} diff --git a/src/lib/Persistence/Legacy/Content/Mapper/ResolveVirtualFieldSubscriber.php b/src/lib/Persistence/Legacy/Content/Mapper/ResolveVirtualFieldSubscriber.php index a00ff61f7d..c04fe5dc9e 100644 --- a/src/lib/Persistence/Legacy/Content/Mapper/ResolveVirtualFieldSubscriber.php +++ b/src/lib/Persistence/Legacy/Content/Mapper/ResolveVirtualFieldSubscriber.php @@ -19,6 +19,7 @@ use eZ\Publish\SPI\Persistence\Content\Type\FieldDefinition; use eZ\Publish\SPI\Persistence\Content\VersionInfo; use Ibexa\Contracts\Core\Event\Mapper\ResolveMissingFieldEvent; +use Ibexa\Contracts\FieldType\DefaultDataFieldStorage; use Symfony\Component\EventDispatcher\EventSubscriberInterface; final class ResolveVirtualFieldSubscriber implements EventSubscriberInterface @@ -47,6 +48,7 @@ public static function getSubscribedEvents(): array return [ ResolveMissingFieldEvent::class => [ ['persistExternalStorageField', -100], + ['resolveVirtualExternalStorageField', -80], ['resolveVirtualField', 0], ], ]; @@ -73,6 +75,9 @@ public function resolveVirtualField(ResolveMissingFieldEvent $event): void } } + /** + * @throws \eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\Exception\NotFound + */ public function persistExternalStorageField(ResolveMissingFieldEvent $event): void { $field = $event->getField(); @@ -98,13 +103,61 @@ public function persistExternalStorageField(ResolveMissingFieldEvent $event): vo $this->getDefaultStorageValue() ); - $storage->getFieldData( + $result = $storage->storeFieldData( $content->versionInfo, $field, $event->getContext() ); + if ($result === true) { + $storageValue = new StorageFieldValue(); + $converter = $this->converterRegistry->getConverter($fieldDefinition->fieldType); + $converter->toStorageValue( + $field->value, + $storageValue + ); + + $this->contentGateway->updateField( + $field, + $storageValue + ); + } + + $event->setField($field); + } + + public function resolveVirtualExternalStorageField(ResolveMissingFieldEvent $event): void + { + $field = $event->getField(); + + if ($field && $field->id !== null) { + // Not a virtual field + return; + } + + $fieldDefinition = $event->getFieldDefinition(); + $storage = $this->storageRegistry->getStorage($fieldDefinition->fieldType); + + if ($storage instanceof NullStorage) { + // Not an external storage + return; + } + + if (!$storage instanceof DefaultDataFieldStorage) { + return; + } + + $content = $event->getContent(); + + $storage->getDefaultFieldData( + $content->versionInfo, + $field + ); + $event->setField($field); + + // Do not persist the external storage field + $event->stopPropagation(); } /**