diff --git a/eZ/Publish/Core/Persistence/Legacy/Content/FieldHandler.php b/eZ/Publish/Core/Persistence/Legacy/Content/FieldHandler.php index dba486b78e..0be175d5f4 100644 --- a/eZ/Publish/Core/Persistence/Legacy/Content/FieldHandler.php +++ b/eZ/Publish/Core/Persistence/Legacy/Content/FieldHandler.php @@ -332,7 +332,7 @@ public function updateFields(Content $content, UpdateStruct $updateStruct, Type if (isset($updateFieldMap[$fieldDefinition->id][$languageCode])) { $field = clone $updateFieldMap[$fieldDefinition->id][$languageCode]; $field->versionNo = $content->versionInfo->versionNo; - if (isset($field->id) && array_key_exists($field->languageCode, $existingLanguageCodes)) { + if (null !== $field->id && array_key_exists($field->languageCode, $existingLanguageCodes)) { $this->updateField($field, $content); $updatedFields[$fieldDefinition->id][$languageCode] = $field; } else { @@ -358,6 +358,14 @@ public function updateFields(Content $content, UpdateStruct $updateStruct, Type // also update copied field data // Register for processing after all given fields are updated $nonTranslatableCopiesUpdateSet[$fieldDefinition->id][] = $languageCode; + } elseif (isset($contentFieldMap[$fieldDefinition->id][$languageCode])) { + $field = clone $contentFieldMap[$fieldDefinition->id][$languageCode]; + $field->versionNo = $content->versionInfo->versionNo; + // Persist virtual field + if (null === $field->id) { + $this->updateField($field, $content); + $updatedFields[$fieldDefinition->id][$languageCode] = $field; + } } // If no above conditions were met - do nothing @@ -417,7 +425,7 @@ protected function updateCopiedField(Field $field, Field $updateField, Field $or * @param \eZ\Publish\SPI\Persistence\Content\Field[] $fields * @param array $languageCodes * - * @return \eZ\Publish\SPI\Persistence\Content\Field[][] + * @return array> */ protected function getFieldMap(array $fields, &$languageCodes = null) { diff --git a/eZ/Publish/Core/Persistence/Legacy/Content/Handler.php b/eZ/Publish/Core/Persistence/Legacy/Content/Handler.php index f045281f00..677f75af20 100644 --- a/eZ/Publish/Core/Persistence/Legacy/Content/Handler.php +++ b/eZ/Publish/Core/Persistence/Legacy/Content/Handler.php @@ -15,6 +15,7 @@ use eZ\Publish\SPI\Persistence\Content\CreateStruct; use eZ\Publish\SPI\Persistence\Content\Field; use eZ\Publish\SPI\Persistence\Content\Handler as BaseContentHandler; +use eZ\Publish\SPI\Persistence\Content\Language\Handler as LanguageHandler; use eZ\Publish\SPI\Persistence\Content\MetadataUpdateStruct; use eZ\Publish\SPI\Persistence\Content\Relation\CreateStruct as RelationCreateStruct; use eZ\Publish\SPI\Persistence\Content\Type\Handler as ContentTypeHandler; @@ -84,6 +85,8 @@ class Handler implements BaseContentHandler */ protected $treeHandler; + protected LanguageHandler $languageHandler; + /** @var \Psr\Log\LoggerInterface */ private $logger; @@ -109,6 +112,7 @@ public function __construct( UrlAliasGateway $urlAliasGateway, ContentTypeHandler $contentTypeHandler, TreeHandler $treeHandler, + LanguageHandler $languageHandler, LoggerInterface $logger = null ) { $this->contentGateway = $contentGateway; @@ -119,6 +123,7 @@ public function __construct( $this->urlAliasGateway = $urlAliasGateway; $this->contentTypeHandler = $contentTypeHandler; $this->treeHandler = $treeHandler; + $this->languageHandler = $languageHandler; $this->logger = null !== $logger ? $logger : new NullLogger(); } @@ -275,6 +280,14 @@ public function createDraftFromVersion($contentId, $srcVersion, $userId, ?string // Clone fields from previous version and append them to the new one $this->fieldHandler->createExistingFieldsInNewVersion($content); + // Persist virtual fields + $contentType = $this->contentTypeHandler->load($content->versionInfo->contentInfo->contentTypeId); + $this->fieldHandler->updateFields($content, new UpdateStruct([ + 'initialLanguageId' => $this->languageHandler->loadByLanguageCode( + $content->versionInfo->initialLanguageCode + )->id, + ]), $contentType); + // Create relations for new version $relations = $this->contentGateway->loadRelations($contentId, $srcVersion); foreach ($relations as $relation) { diff --git a/eZ/Publish/Core/Persistence/Legacy/Content/Mapper.php b/eZ/Publish/Core/Persistence/Legacy/Content/Mapper.php index 6a357afe69..ed9e8a8dbc 100644 --- a/eZ/Publish/Core/Persistence/Legacy/Content/Mapper.php +++ b/eZ/Publish/Core/Persistence/Legacy/Content/Mapper.php @@ -345,6 +345,8 @@ private function buildContentObjects( } /** + * @param string[]|null $translations + * * @phpstan-return TVersionedLanguageFieldDefinitionsMap * * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException @@ -374,7 +376,8 @@ private function loadCachedVersionFieldDefinitionsPerLanguage( $contentType = $contentTypes[$contentTypeId]; foreach ($contentType->fieldDefinitions as $fieldDefinition) { foreach ($languageCodes as $languageCode) { - $id = $fieldDefinition->id; + $id = (int)$fieldDefinition->id; + $languageCode = (string)$languageCode; $fieldDefinitions[$contentId][$versionId][$languageCode][$id] = $fieldDefinition; } } diff --git a/eZ/Publish/Core/Persistence/Legacy/Tests/Content/ContentHandlerTest.php b/eZ/Publish/Core/Persistence/Legacy/Tests/Content/ContentHandlerTest.php index ce2720aab0..69548ce248 100644 --- a/eZ/Publish/Core/Persistence/Legacy/Tests/Content/ContentHandlerTest.php +++ b/eZ/Publish/Core/Persistence/Legacy/Tests/Content/ContentHandlerTest.php @@ -11,6 +11,7 @@ use eZ\Publish\Core\Persistence\Legacy\Content\FieldHandler; use eZ\Publish\Core\Persistence\Legacy\Content\Gateway as ContentGateway; use eZ\Publish\Core\Persistence\Legacy\Content\Handler; +use eZ\Publish\Core\Persistence\Legacy\Content\Language\Handler as LanguageHandler; use eZ\Publish\Core\Persistence\Legacy\Content\Location\Gateway as LocationGateway; use eZ\Publish\Core\Persistence\Legacy\Content\Mapper; use eZ\Publish\Core\Persistence\Legacy\Content\TreeHandler; @@ -108,6 +109,8 @@ class ContentHandlerTest extends TestCase */ protected $contentTypeHandlerMock; + protected LanguageHandler $languageHandlerMock; + /** * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Handler::create * @@ -384,6 +387,8 @@ public function testCreateDraftFromVersion() $mapperMock = $this->getMapperMock(); $gatewayMock = $this->getGatewayMock(); $fieldHandlerMock = $this->getFieldHandlerMock(); + $languageHandlerMock = $this->getLanguageHandlerMock(); + $contentTypeHandlerMock = $this->getContentTypeHandlerMock(); $handler->expects($this->once()) ->method('load') @@ -402,11 +407,18 @@ public function testCreateDraftFromVersion() [ 'names' => [], 'versionNo' => 3, + 'contentInfo' => new ContentInfo(), ] ) ) ); + $languageHandlerMock->method('loadByLanguageCode') + ->willReturn(new Content\Language()); + + $contentTypeHandlerMock->method('load') + ->willReturn(new Type()); + $gatewayMock->expects($this->once()) ->method('insertVersion') ->with( @@ -1538,7 +1550,8 @@ protected function getContentHandler() $this->getSlugConverterMock(), $this->getUrlAliasGatewayMock(), $this->getContentTypeHandlerMock(), - $this->getTreeHandlerMock() + $this->getTreeHandlerMock(), + $this->getLanguageHandlerMock(), ); } @@ -1566,6 +1579,7 @@ protected function getPartlyMockedHandler(array $methods) $this->getUrlAliasGatewayMock(), $this->getContentTypeHandlerMock(), $this->getTreeHandlerMock(), + $this->getLanguageHandlerMock(), ] ) ->getMock(); @@ -1599,6 +1613,18 @@ protected function getContentTypeHandlerMock() return $this->contentTypeHandlerMock; } + /** + * @return \PHPUnit\Framework\MockObject\MockObject|\eZ\Publish\Core\Persistence\Legacy\Content\Language\Handler + */ + protected function getLanguageHandlerMock(): LanguageHandler + { + if (!isset($this->languageHandlerMock)) { + $this->languageHandlerMock = $this->createMock(LanguageHandler::class); + } + + return $this->languageHandlerMock; + } + /** * Returns a FieldHandler mock. * diff --git a/eZ/Publish/Core/Persistence/Legacy/Tests/Content/FieldHandlerTest.php b/eZ/Publish/Core/Persistence/Legacy/Tests/Content/FieldHandlerTest.php index d784832cad..93b010529c 100644 --- a/eZ/Publish/Core/Persistence/Legacy/Tests/Content/FieldHandlerTest.php +++ b/eZ/Publish/Core/Persistence/Legacy/Tests/Content/FieldHandlerTest.php @@ -633,7 +633,7 @@ public function assertUpdateFieldsForInitialLanguage($storageHandlerUpdatesField $callNo = 0; $fieldValue = new FieldValue(); $fieldsToCopy = []; - foreach ([1, 2, 3] as $fieldDefinitionId) { + foreach ([1, 2, 3] as $id => $fieldDefinitionId) { $field = new Field( [ 'fieldDefinitionId' => $fieldDefinitionId, @@ -646,6 +646,7 @@ public function assertUpdateFieldsForInitialLanguage($storageHandlerUpdatesField // These fields are copied from main language if ($fieldDefinitionId == 2 || $fieldDefinitionId == 3) { $originalField = clone $field; + $originalField->id = $fieldDefinitionId; $originalField->languageCode = 'eng-GB'; $fieldsToCopy[] = [ 'copy' => clone $field, @@ -845,20 +846,13 @@ protected function getContentSingleLanguageFixture() $field->value = new FieldValue(); $field->languageCode = 'eng-GB'; - $firstField = clone $field; - $firstField->fieldDefinitionId = 1; - - $secondField = clone $field; - $secondField->fieldDefinitionId = 2; - - $thirdField = clone $field; - $thirdField->fieldDefinitionId = 3; + foreach ([1, 2, 3] as $id) { + $contentField = clone $field; + $contentField->id = $id; + $contentField->fieldDefinitionId = $id; - $content->fields = [ - $firstField, - $secondField, - $thirdField, - ]; + $content->fields[] = $contentField; + } return $content; } diff --git a/eZ/Publish/Core/settings/storage_engines/legacy/content.yml b/eZ/Publish/Core/settings/storage_engines/legacy/content.yml index 58c0de94b0..2a956461b7 100644 --- a/eZ/Publish/Core/settings/storage_engines/legacy/content.yml +++ b/eZ/Publish/Core/settings/storage_engines/legacy/content.yml @@ -68,5 +68,6 @@ services: - "@ezpublish.persistence.legacy.url_alias.gateway" - "@ezpublish.spi.persistence.legacy.content_type.handler" - "@ezpublish.persistence.legacy.tree_handler" + - "@ezpublish.spi.persistence.legacy.language.handler" - "@logger" lazy: true