Skip to content

Commit

Permalink
fix: Makes single field contents editable after field switch
Browse files Browse the repository at this point in the history
  • Loading branch information
Nattfarinn committed Jul 31, 2024
1 parent 18b7369 commit 3d1f30c
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 19 deletions.
12 changes: 10 additions & 2 deletions eZ/Publish/Core/Persistence/Legacy/Content/FieldHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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<int, array<string, \eZ\Publish\SPI\Persistence\Content\Field>>
*/
protected function getFieldMap(array $fields, &$languageCodes = null)
{
Expand Down
14 changes: 13 additions & 1 deletion eZ/Publish/Core/Persistence/Legacy/Content/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
use eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\SlugConverter;
use eZ\Publish\SPI\Persistence\Content;
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;
Expand Down Expand Up @@ -84,6 +84,8 @@ class Handler implements BaseContentHandler
*/
protected $treeHandler;

protected LanguageHandler $languageHandler;

/** @var \Psr\Log\LoggerInterface */
private $logger;

Expand All @@ -109,6 +111,7 @@ public function __construct(
UrlAliasGateway $urlAliasGateway,
ContentTypeHandler $contentTypeHandler,
TreeHandler $treeHandler,
LanguageHandler $languageHandler,
LoggerInterface $logger = null
) {
$this->contentGateway = $contentGateway;
Expand All @@ -119,6 +122,7 @@ public function __construct(
$this->urlAliasGateway = $urlAliasGateway;
$this->contentTypeHandler = $contentTypeHandler;
$this->treeHandler = $treeHandler;
$this->languageHandler = $languageHandler;
$this->logger = null !== $logger ? $logger : new NullLogger();
}

Expand Down Expand Up @@ -275,6 +279,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) {
Expand Down
5 changes: 4 additions & 1 deletion eZ/Publish/Core/Persistence/Legacy/Content/Mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,8 @@ private function buildContentObjects(
}

/**
* @param string[]|null $translations
*
* @phpstan-return TVersionedLanguageFieldDefinitionsMap
*
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
Expand Down Expand Up @@ -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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -108,6 +109,8 @@ class ContentHandlerTest extends TestCase
*/
protected $contentTypeHandlerMock;

protected LanguageHandler $languageHandlerMock;

/**
* @covers \eZ\Publish\Core\Persistence\Legacy\Content\Handler::create
*
Expand Down Expand Up @@ -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')
Expand All @@ -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(
Expand Down Expand Up @@ -1538,7 +1550,8 @@ protected function getContentHandler()
$this->getSlugConverterMock(),
$this->getUrlAliasGatewayMock(),
$this->getContentTypeHandlerMock(),
$this->getTreeHandlerMock()
$this->getTreeHandlerMock(),
$this->getLanguageHandlerMock(),
);
}

Expand Down Expand Up @@ -1566,6 +1579,7 @@ protected function getPartlyMockedHandler(array $methods)
$this->getUrlAliasGatewayMock(),
$this->getContentTypeHandlerMock(),
$this->getTreeHandlerMock(),
$this->getLanguageHandlerMock(),
]
)
->getMock();
Expand Down Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 3d1f30c

Please sign in to comment.