Skip to content

Commit 189e046

Browse files
committed
fix: interface DefaultDataFieldStorage
1 parent 70d1af8 commit 189e046

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
/**
4+
* @copyright Copyright (C) Ibexa AS. All rights reserved.
5+
* @license For full copyright and license information view LICENSE file distributed with this source code.
6+
*/
7+
declare(strict_types=1);
8+
9+
namespace Ibexa\Contracts\FieldType;
10+
11+
use eZ\Publish\SPI\Persistence\Content\Field;
12+
use eZ\Publish\SPI\Persistence\Content\VersionInfo;
13+
14+
interface DefaultDataFieldStorage
15+
{
16+
/**
17+
* Populates $field value property with default data based on the external data.
18+
* $field->value is a {@link \eZ\Publish\SPI\Persistence\Content\FieldValue} object.
19+
* This value holds the data as a {@link \eZ\Publish\Core\FieldType\Value} based object, according to
20+
* the field type (e.g. for TextLine, it will be a {@link \eZ\Publish\Core\FieldType\TextLine\Value} object).
21+
*
22+
* @param \eZ\Publish\SPI\Persistence\Content\VersionInfo $versionInfo
23+
* @param \eZ\Publish\SPI\Persistence\Content\Field $field
24+
*/
25+
public function getDefaultFieldData(VersionInfo $versionInfo, Field $field): void;
26+
}

src/lib/Persistence/Legacy/Content/Mapper/ResolveVirtualFieldSubscriber.php

+54-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use eZ\Publish\SPI\Persistence\Content\Type\FieldDefinition;
2020
use eZ\Publish\SPI\Persistence\Content\VersionInfo;
2121
use Ibexa\Contracts\Core\Event\Mapper\ResolveMissingFieldEvent;
22+
use Ibexa\Contracts\FieldType\DefaultDataFieldStorage;
2223
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
2324

2425
final class ResolveVirtualFieldSubscriber implements EventSubscriberInterface
@@ -47,6 +48,7 @@ public static function getSubscribedEvents(): array
4748
return [
4849
ResolveMissingFieldEvent::class => [
4950
['persistExternalStorageField', -100],
51+
['resolveVirtualExternalStorageField', -80],
5052
['resolveVirtualField', 0],
5153
],
5254
];
@@ -73,6 +75,9 @@ public function resolveVirtualField(ResolveMissingFieldEvent $event): void
7375
}
7476
}
7577

78+
/**
79+
* @throws \eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\Exception\NotFound
80+
*/
7681
public function persistExternalStorageField(ResolveMissingFieldEvent $event): void
7782
{
7883
$field = $event->getField();
@@ -98,13 +103,61 @@ public function persistExternalStorageField(ResolveMissingFieldEvent $event): vo
98103
$this->getDefaultStorageValue()
99104
);
100105

101-
$storage->getFieldData(
106+
$result = $storage->storeFieldData(
102107
$content->versionInfo,
103108
$field,
104109
$event->getContext()
105110
);
106111

112+
if ($result === true) {
113+
$storageValue = new StorageFieldValue();
114+
$converter = $this->converterRegistry->getConverter($fieldDefinition->fieldType);
115+
$converter->toStorageValue(
116+
$field->value,
117+
$storageValue
118+
);
119+
120+
$this->contentGateway->updateField(
121+
$field,
122+
$storageValue
123+
);
124+
}
125+
126+
$event->setField($field);
127+
}
128+
129+
public function resolveVirtualExternalStorageField(ResolveMissingFieldEvent $event): void
130+
{
131+
$field = $event->getField();
132+
133+
if ($field && $field->id !== null) {
134+
// Not a virtual field
135+
return;
136+
}
137+
138+
$fieldDefinition = $event->getFieldDefinition();
139+
$storage = $this->storageRegistry->getStorage($fieldDefinition->fieldType);
140+
141+
if ($storage instanceof NullStorage) {
142+
// Not an external storage
143+
return;
144+
}
145+
146+
if (!$storage instanceof DefaultDataFieldStorage) {
147+
return;
148+
}
149+
150+
$content = $event->getContent();
151+
152+
$storage->getDefaultFieldData(
153+
$content->versionInfo,
154+
$field
155+
);
156+
107157
$event->setField($field);
158+
159+
// Do not persist the external storage field
160+
$event->stopPropagation();
108161
}
109162

110163
/**

0 commit comments

Comments
 (0)