Skip to content

Commit 36b051b

Browse files
committed
fix: Makes single field contents editable after field switch
1 parent 18b7369 commit 36b051b

File tree

6 files changed

+63
-18
lines changed

6 files changed

+63
-18
lines changed

eZ/Publish/Core/Persistence/Legacy/Content/FieldHandler.php

+10-2
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ public function updateFields(Content $content, UpdateStruct $updateStruct, Type
332332
if (isset($updateFieldMap[$fieldDefinition->id][$languageCode])) {
333333
$field = clone $updateFieldMap[$fieldDefinition->id][$languageCode];
334334
$field->versionNo = $content->versionInfo->versionNo;
335-
if (isset($field->id) && array_key_exists($field->languageCode, $existingLanguageCodes)) {
335+
if (null !== $field->id && array_key_exists($field->languageCode, $existingLanguageCodes)) {
336336
$this->updateField($field, $content);
337337
$updatedFields[$fieldDefinition->id][$languageCode] = $field;
338338
} else {
@@ -358,6 +358,14 @@ public function updateFields(Content $content, UpdateStruct $updateStruct, Type
358358
// also update copied field data
359359
// Register for processing after all given fields are updated
360360
$nonTranslatableCopiesUpdateSet[$fieldDefinition->id][] = $languageCode;
361+
} elseif (isset($contentFieldMap[$fieldDefinition->id][$languageCode])) {
362+
$field = clone $contentFieldMap[$fieldDefinition->id][$languageCode];
363+
$field->versionNo = $content->versionInfo->versionNo;
364+
// Persist virtual field
365+
if (null === $field->id) {
366+
$this->updateField($field, $content);
367+
$updatedFields[$fieldDefinition->id][$languageCode] = $field;
368+
}
361369
}
362370

363371
// If no above conditions were met - do nothing
@@ -417,7 +425,7 @@ protected function updateCopiedField(Field $field, Field $updateField, Field $or
417425
* @param \eZ\Publish\SPI\Persistence\Content\Field[] $fields
418426
* @param array $languageCodes
419427
*
420-
* @return \eZ\Publish\SPI\Persistence\Content\Field[][]
428+
* @return array<int, array<string, \eZ\Publish\SPI\Persistence\Content\Field>>
421429
*/
422430
protected function getFieldMap(array $fields, &$languageCodes = null)
423431
{

eZ/Publish/Core/Persistence/Legacy/Content/Handler.php

+13
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use eZ\Publish\SPI\Persistence\Content\CreateStruct;
1616
use eZ\Publish\SPI\Persistence\Content\Field;
1717
use eZ\Publish\SPI\Persistence\Content\Handler as BaseContentHandler;
18+
use eZ\Publish\SPI\Persistence\Content\Language\Handler as LanguageHandler;
1819
use eZ\Publish\SPI\Persistence\Content\MetadataUpdateStruct;
1920
use eZ\Publish\SPI\Persistence\Content\Relation\CreateStruct as RelationCreateStruct;
2021
use eZ\Publish\SPI\Persistence\Content\Type\Handler as ContentTypeHandler;
@@ -84,6 +85,8 @@ class Handler implements BaseContentHandler
8485
*/
8586
protected $treeHandler;
8687

88+
protected LanguageHandler $languageHandler;
89+
8790
/** @var \Psr\Log\LoggerInterface */
8891
private $logger;
8992

@@ -109,6 +112,7 @@ public function __construct(
109112
UrlAliasGateway $urlAliasGateway,
110113
ContentTypeHandler $contentTypeHandler,
111114
TreeHandler $treeHandler,
115+
LanguageHandler $languageHandler,
112116
LoggerInterface $logger = null
113117
) {
114118
$this->contentGateway = $contentGateway;
@@ -119,6 +123,7 @@ public function __construct(
119123
$this->urlAliasGateway = $urlAliasGateway;
120124
$this->contentTypeHandler = $contentTypeHandler;
121125
$this->treeHandler = $treeHandler;
126+
$this->languageHandler = $languageHandler;
122127
$this->logger = null !== $logger ? $logger : new NullLogger();
123128
}
124129

@@ -275,6 +280,14 @@ public function createDraftFromVersion($contentId, $srcVersion, $userId, ?string
275280
// Clone fields from previous version and append them to the new one
276281
$this->fieldHandler->createExistingFieldsInNewVersion($content);
277282

283+
// Persist virtual fields
284+
$contentType = $this->contentTypeHandler->load($content->versionInfo->contentInfo->contentTypeId);
285+
$this->fieldHandler->updateFields($content, new UpdateStruct([
286+
'initialLanguageId' => $this->languageHandler->loadByLanguageCode(
287+
$content->versionInfo->initialLanguageCode
288+
)->id,
289+
]), $contentType);
290+
278291
// Create relations for new version
279292
$relations = $this->contentGateway->loadRelations($contentId, $srcVersion);
280293
foreach ($relations as $relation) {

eZ/Publish/Core/Persistence/Legacy/Content/Mapper.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,8 @@ private function buildContentObjects(
345345
}
346346

347347
/**
348+
* @param string[]|null $translations
349+
*
348350
* @phpstan-return TVersionedLanguageFieldDefinitionsMap
349351
*
350352
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
@@ -374,7 +376,8 @@ private function loadCachedVersionFieldDefinitionsPerLanguage(
374376
$contentType = $contentTypes[$contentTypeId];
375377
foreach ($contentType->fieldDefinitions as $fieldDefinition) {
376378
foreach ($languageCodes as $languageCode) {
377-
$id = $fieldDefinition->id;
379+
$id = (int)$fieldDefinition->id;
380+
$languageCode = (string)$languageCode;
378381
$fieldDefinitions[$contentId][$versionId][$languageCode][$id] = $fieldDefinition;
379382
}
380383
}

eZ/Publish/Core/Persistence/Legacy/Tests/Content/ContentHandlerTest.php

+27-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use eZ\Publish\Core\Persistence\Legacy\Content\FieldHandler;
1212
use eZ\Publish\Core\Persistence\Legacy\Content\Gateway as ContentGateway;
1313
use eZ\Publish\Core\Persistence\Legacy\Content\Handler;
14+
use eZ\Publish\Core\Persistence\Legacy\Content\Language\Handler as LanguageHandler;
1415
use eZ\Publish\Core\Persistence\Legacy\Content\Location\Gateway as LocationGateway;
1516
use eZ\Publish\Core\Persistence\Legacy\Content\Mapper;
1617
use eZ\Publish\Core\Persistence\Legacy\Content\TreeHandler;
@@ -108,6 +109,8 @@ class ContentHandlerTest extends TestCase
108109
*/
109110
protected $contentTypeHandlerMock;
110111

112+
protected LanguageHandler $languageHandlerMock;
113+
111114
/**
112115
* @covers \eZ\Publish\Core\Persistence\Legacy\Content\Handler::create
113116
*
@@ -384,6 +387,8 @@ public function testCreateDraftFromVersion()
384387
$mapperMock = $this->getMapperMock();
385388
$gatewayMock = $this->getGatewayMock();
386389
$fieldHandlerMock = $this->getFieldHandlerMock();
390+
$languageHandlerMock = $this->getLanguageHandlerMock();
391+
$contentTypeHandlerMock = $this->getContentTypeHandlerMock();
387392

388393
$handler->expects($this->once())
389394
->method('load')
@@ -402,11 +407,18 @@ public function testCreateDraftFromVersion()
402407
[
403408
'names' => [],
404409
'versionNo' => 3,
410+
'contentInfo' => new ContentInfo(),
405411
]
406412
)
407413
)
408414
);
409415

416+
$languageHandlerMock->method('loadByLanguageCode')
417+
->willReturn(new Content\Language());
418+
419+
$contentTypeHandlerMock->method('load')
420+
->willReturn(new Type());
421+
410422
$gatewayMock->expects($this->once())
411423
->method('insertVersion')
412424
->with(
@@ -1538,7 +1550,8 @@ protected function getContentHandler()
15381550
$this->getSlugConverterMock(),
15391551
$this->getUrlAliasGatewayMock(),
15401552
$this->getContentTypeHandlerMock(),
1541-
$this->getTreeHandlerMock()
1553+
$this->getTreeHandlerMock(),
1554+
$this->getLanguageHandlerMock(),
15421555
);
15431556
}
15441557

@@ -1566,6 +1579,7 @@ protected function getPartlyMockedHandler(array $methods)
15661579
$this->getUrlAliasGatewayMock(),
15671580
$this->getContentTypeHandlerMock(),
15681581
$this->getTreeHandlerMock(),
1582+
$this->getLanguageHandlerMock(),
15691583
]
15701584
)
15711585
->getMock();
@@ -1599,6 +1613,18 @@ protected function getContentTypeHandlerMock()
15991613
return $this->contentTypeHandlerMock;
16001614
}
16011615

1616+
/**
1617+
* @return \PHPUnit\Framework\MockObject\MockObject|\eZ\Publish\Core\Persistence\Legacy\Content\Language\Handler
1618+
*/
1619+
protected function getLanguageHandlerMock(): LanguageHandler
1620+
{
1621+
if (!isset($this->languageHandlerMock)) {
1622+
$this->languageHandlerMock = $this->createMock(LanguageHandler::class);
1623+
}
1624+
1625+
return $this->languageHandlerMock;
1626+
}
1627+
16021628
/**
16031629
* Returns a FieldHandler mock.
16041630
*

eZ/Publish/Core/Persistence/Legacy/Tests/Content/FieldHandlerTest.php

+8-14
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ public function assertUpdateFieldsForInitialLanguage($storageHandlerUpdatesField
633633
$callNo = 0;
634634
$fieldValue = new FieldValue();
635635
$fieldsToCopy = [];
636-
foreach ([1, 2, 3] as $fieldDefinitionId) {
636+
foreach ([1, 2, 3] as $id => $fieldDefinitionId) {
637637
$field = new Field(
638638
[
639639
'fieldDefinitionId' => $fieldDefinitionId,
@@ -646,6 +646,7 @@ public function assertUpdateFieldsForInitialLanguage($storageHandlerUpdatesField
646646
// These fields are copied from main language
647647
if ($fieldDefinitionId == 2 || $fieldDefinitionId == 3) {
648648
$originalField = clone $field;
649+
$originalField->id = $fieldDefinitionId;
649650
$originalField->languageCode = 'eng-GB';
650651
$fieldsToCopy[] = [
651652
'copy' => clone $field,
@@ -845,20 +846,13 @@ protected function getContentSingleLanguageFixture()
845846
$field->value = new FieldValue();
846847
$field->languageCode = 'eng-GB';
847848

848-
$firstField = clone $field;
849-
$firstField->fieldDefinitionId = 1;
850-
851-
$secondField = clone $field;
852-
$secondField->fieldDefinitionId = 2;
853-
854-
$thirdField = clone $field;
855-
$thirdField->fieldDefinitionId = 3;
849+
foreach ([1, 2, 3] as $id) {
850+
$contentField = clone $field;
851+
$contentField->id = $id;
852+
$contentField->fieldDefinitionId = $id;
856853

857-
$content->fields = [
858-
$firstField,
859-
$secondField,
860-
$thirdField,
861-
];
854+
$content->fields[] = $contentField;
855+
}
862856

863857
return $content;
864858
}

eZ/Publish/Core/settings/storage_engines/legacy/content.yml

+1
Original file line numberDiff line numberDiff line change
@@ -68,5 +68,6 @@ services:
6868
- "@ezpublish.persistence.legacy.url_alias.gateway"
6969
- "@ezpublish.spi.persistence.legacy.content_type.handler"
7070
- "@ezpublish.persistence.legacy.tree_handler"
71+
- "@ezpublish.spi.persistence.legacy.language.handler"
7172
- "@logger"
7273
lazy: true

0 commit comments

Comments
 (0)