-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2485639
commit e24c72b
Showing
4 changed files
with
246 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
Check warning on line 1 in src/contracts/Event/Mapper/ResolveMissingFieldEvent.php GitHub Actions / Run code style check (8.0)
Check warning on line 1 in src/contracts/Event/Mapper/ResolveMissingFieldEvent.php GitHub Actions / Run code style check (8.0)
|
||
|
||
namespace Ibexa\Contracts\Core\Event\Mapper; | ||
|
||
use eZ\Publish\SPI\Persistence\Content; | ||
use eZ\Publish\SPI\Persistence\Content\Field; | ||
use eZ\Publish\SPI\Persistence\Content\Type\FieldDefinition; | ||
use Symfony\Contracts\EventDispatcher\Event; | ||
|
||
final class ResolveMissingFieldEvent extends Event | ||
{ | ||
/** @var Content */ | ||
private $content; | ||
|
||
/** @var FieldDefinition */ | ||
private $fieldDefinition; | ||
|
||
/** @var string */ | ||
private $languageCode; | ||
|
||
/** @var array */ | ||
private $context; | ||
|
||
/** @var Field|null */ | ||
private $field; | ||
|
||
public function __construct( | ||
Content $content, | ||
FieldDefinition $fieldDefinition, | ||
string $languageCode, | ||
array $context = [] | ||
) { | ||
$this->content = $content; | ||
$this->fieldDefinition = $fieldDefinition; | ||
$this->languageCode = $languageCode; | ||
$this->context = $context; | ||
$this->field = null; | ||
} | ||
|
||
public function getContent(): Content | ||
{ | ||
return $this->content; | ||
} | ||
|
||
public function getFieldDefinition(): FieldDefinition | ||
{ | ||
return $this->fieldDefinition; | ||
} | ||
|
||
public function getLanguageCode(): string | ||
{ | ||
return $this->languageCode; | ||
} | ||
|
||
public function getContext(): array | ||
{ | ||
return $this->context; | ||
} | ||
|
||
public function setField(?Field $field): void | ||
{ | ||
$this->field = $field; | ||
} | ||
|
||
public function getField(): ?Field | ||
{ | ||
return $this->field; | ||
} | ||
} |
154 changes: 154 additions & 0 deletions
154
src/lib/Persistence/Legacy/Content/Mapper/ResolveVirtualFieldSubscriber.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
<?php | ||
Check warning on line 1 in src/lib/Persistence/Legacy/Content/Mapper/ResolveVirtualFieldSubscriber.php GitHub Actions / Run code style check (8.0)
|
||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Ibexa\Core\Persistence\Legacy\Content\Mapper; | ||
|
||
use eZ\Publish\Core\FieldType\NullStorage; | ||
use eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\Exception\NotFound; | ||
use eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\ConverterRegistry; | ||
use eZ\Publish\Core\Persistence\Legacy\Content\Gateway as ContentGateway; | ||
use eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue; | ||
use eZ\Publish\Core\Persistence\Legacy\Content\StorageRegistry; | ||
use eZ\Publish\SPI\Persistence\Content\Field; | ||
use eZ\Publish\SPI\Persistence\Content\FieldValue; | ||
use eZ\Publish\SPI\Persistence\Content\Type\FieldDefinition; | ||
use eZ\Publish\SPI\Persistence\Content\VersionInfo; | ||
use Ibexa\Contracts\Core\Event\Mapper\ResolveMissingFieldEvent; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
|
||
final class ResolveVirtualFieldSubscriber implements EventSubscriberInterface | ||
{ | ||
/** @var ConverterRegistry */ | ||
private $converterRegistry; | ||
|
||
/** @var StorageRegistry */ | ||
private $storageRegistry; | ||
|
||
/** @var ContentGateway */ | ||
private $contentGateway; | ||
|
||
public function __construct( | ||
ConverterRegistry $converterRegistry, | ||
StorageRegistry $storageRegistry, | ||
ContentGateway $contentGateway | ||
) { | ||
$this->converterRegistry = $converterRegistry; | ||
$this->storageRegistry = $storageRegistry; | ||
$this->contentGateway = $contentGateway; | ||
} | ||
|
||
public static function getSubscribedEvents(): array | ||
{ | ||
return [ | ||
ResolveMissingFieldEvent::class => [ | ||
['persistExternalStorageField', -100], | ||
['resolveVirtualField', 0], | ||
] | ||
]; | ||
} | ||
|
||
public function resolveVirtualField(ResolveMissingFieldEvent $event): void | ||
{ | ||
if ($event->getField()) { | ||
return; | ||
} | ||
|
||
$content = $event->getContent(); | ||
|
||
try { | ||
$emptyField = $this->createEmptyField( | ||
$content->versionInfo, | ||
$event->getFieldDefinition(), | ||
$event->getLanguageCode() | ||
); | ||
|
||
$event->setField($emptyField); | ||
} catch (NotFound $exception) { | ||
return; | ||
} | ||
} | ||
|
||
public function persistExternalStorageField(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; | ||
} | ||
|
||
$content = $event->getContent(); | ||
|
||
$field->id = $this->contentGateway->insertNewField( | ||
$content, | ||
$field, | ||
$this->getDefaultStorageValue() | ||
); | ||
|
||
$storage->getFieldData( | ||
$content->versionInfo, | ||
$field, | ||
$event->getContext() | ||
); | ||
|
||
$event->setField($field); | ||
} | ||
|
||
/** | ||
* @throws NotFound | ||
*/ | ||
private function createEmptyField( | ||
VersionInfo $versionInfo, | ||
FieldDefinition $fieldDefinition, | ||
string $languageCode | ||
): Field { | ||
$field = new Field(); | ||
$field->fieldDefinitionId = $fieldDefinition->id; | ||
$field->type = $fieldDefinition->fieldType; | ||
$field->value = $this->getDefaultValue($fieldDefinition); | ||
$field->languageCode = $languageCode; | ||
$field->versionNo = $versionInfo->versionNo; | ||
|
||
return $field; | ||
} | ||
|
||
/** | ||
* @throws NotFound | ||
*/ | ||
private function getDefaultValue(FieldDefinition $fieldDefinition): FieldValue | ||
{ | ||
$value = clone $fieldDefinition->defaultValue; | ||
$storageValue = $this->getDefaultStorageValue(); | ||
|
||
$converter = $this->converterRegistry->getConverter($fieldDefinition->fieldType); | ||
$converter->toStorageValue($value, $storageValue); | ||
$converter->toFieldValue($storageValue, $value); | ||
|
||
return $value; | ||
} | ||
|
||
private function getDefaultStorageValue(): StorageFieldValue | ||
{ | ||
$storageValue = new StorageFieldValue(); | ||
$storageValue->dataFloat = null; | ||
$storageValue->dataInt = null; | ||
$storageValue->dataText = ''; | ||
$storageValue->sortKeyInt = 0; | ||
$storageValue->sortKeyString = ''; | ||
|
||
return $storageValue; | ||
} | ||
} |