Skip to content

Commit ea58659

Browse files
authored
IBX-9446: Fixed updating new non-translatable field value (#533)
For more details see https://issues.ibexa.co/browse/IBX-9446 and #533 Key changes: * Fixed processing update of a non-translatable field newly added to a content type
1 parent db39fd7 commit ea58659

File tree

2 files changed

+150
-1
lines changed

2 files changed

+150
-1
lines changed

src/lib/Persistence/Legacy/Content/FieldHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,10 +334,10 @@ public function updateFields(Content $content, UpdateStruct $updateStruct, Type
334334
$field->versionNo = $content->versionInfo->versionNo;
335335
if (isset($field->id) && array_key_exists($field->languageCode, $existingLanguageCodes)) {
336336
$this->updateField($field, $content);
337-
$updatedFields[$fieldDefinition->id][$languageCode] = $field;
338337
} else {
339338
$this->createNewField($field, $content);
340339
}
340+
$updatedFields[$fieldDefinition->id][$languageCode] = $field;
341341
} elseif (!isset($existingLanguageCodes[$languageCode])) {
342342
// If field is not set for new language
343343
if ($fieldDefinition->isTranslatable) {
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
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\Tests\Integration\Core\Persistence\Legacy;
10+
11+
use Ibexa\Contracts\Core\Repository\Values\Content\Content;
12+
use Ibexa\Contracts\Core\Repository\Values\Content\ContentUpdateStruct;
13+
use Ibexa\Contracts\Core\Repository\Values\ContentType\ContentTypeCreateStruct;
14+
use Ibexa\Contracts\Core\Repository\Values\ContentType\FieldDefinitionCreateStruct;
15+
use Ibexa\Tests\Integration\Core\RepositoryTestCase;
16+
17+
final class ContentUpdateAfterAddingFieldDefinitionTest extends RepositoryTestCase
18+
{
19+
/**
20+
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\Exception
21+
*/
22+
public function testUpdateFields(): void
23+
{
24+
$contentService = self::getContentService();
25+
$contentTypeService = self::getContentTypeService();
26+
27+
// Create new ContentType
28+
$fieldDefCreateStruct = $this->createFieldDefinitionStruct('name', 'Name', true);
29+
30+
$contentTypeCreateStruct = $this->createTypeCreateStruct();
31+
$contentTypeCreateStruct->addFieldDefinition($fieldDefCreateStruct);
32+
33+
$contentType = $contentTypeService->createContentType($contentTypeCreateStruct, [
34+
$contentTypeService->loadContentTypeGroupByIdentifier('Content'),
35+
]);
36+
37+
$contentTypeService->publishContentTypeDraft($contentType);
38+
39+
// Create content, with two translations
40+
$content = $this->createNewContent('Some Content', ['eng-US', 'ger-DE']);
41+
42+
// Create draft in language with higher id ( later in the $contentLanguageService->loadLanguages() list than 'eng-US' )
43+
$content = $contentService->loadContent($content->getId(), ['eng-US']);
44+
$engUpdateStruct = $this->createUpdateStruct($content, '', ['eng-US']);
45+
$engDraft = $this->createContentDraft($content, 'eng-US');
46+
$engDraft = $this->updateContent($engDraft, $engUpdateStruct);
47+
48+
// Create new non-translatable field
49+
$contentType = $contentTypeService->loadContentTypeByIdentifier('multi_lang_drafts');
50+
$contentTypeDraft = $contentTypeService->createContentTypeDraft($contentType);
51+
$fieldDefCreateStruct = $this->createFieldDefinitionStruct('non_trans_field', 'Non translatable field', false);
52+
$contentTypeService->addFieldDefinition($contentTypeDraft, $fieldDefCreateStruct);
53+
54+
$contentTypeService->publishContentTypeDraft($contentTypeDraft);
55+
56+
// Update eng-US draft
57+
$engUpdateStruct->setField('non_trans_field', '', 'eng-US');
58+
$this->updateContent($engDraft, $engUpdateStruct);
59+
}
60+
61+
private function createFieldDefinitionStruct(string $identifier, string $name, bool $isTranslatable): FieldDefinitionCreateStruct
62+
{
63+
$contentTypeService = self::getContentTypeService();
64+
65+
$fieldDefCreateStruct = $contentTypeService->newFieldDefinitionCreateStruct(
66+
$identifier,
67+
'ezstring'
68+
);
69+
70+
$fieldDefCreateStruct->names = ['eng-US' => $name];
71+
$fieldDefCreateStruct->descriptions = [
72+
'eng-US' => '',
73+
];
74+
$fieldDefCreateStruct->isTranslatable = $isTranslatable;
75+
76+
return $fieldDefCreateStruct;
77+
}
78+
79+
private function createTypeCreateStruct(): ContentTypeCreateStruct
80+
{
81+
$contentTypeService = self::getContentTypeService();
82+
$typeCreateStruct = $contentTypeService->newContentTypeCreateStruct('multi_lang_drafts');
83+
$typeCreateStruct->mainLanguageCode = 'eng-US';
84+
$typeCreateStruct->names = ['eng-US' => 'Multi lang drafts'];
85+
86+
return $typeCreateStruct;
87+
}
88+
89+
/**
90+
* @param string[] $languages
91+
*/
92+
protected function createNewContent(string $name, array $languages = ['eng-US'], int $parentLocationId = 2): Content
93+
{
94+
$contentTypeService = self::getContentTypeService();
95+
$contentService = self::getContentService();
96+
$locationService = self::getLocationService();
97+
98+
$contentType = $contentTypeService->loadContentTypeByIdentifier('multi_lang_drafts');
99+
$createStruct = $contentService->newContentCreateStruct($contentType, $languages[0]);
100+
101+
foreach ($languages as $language) {
102+
$createStruct->setField('name', "[$language]" . $name, $language);
103+
}
104+
$locationCreateStruct = $locationService->newLocationCreateStruct($parentLocationId);
105+
106+
$draft = $contentService->createContent($createStruct, [$locationCreateStruct]);
107+
108+
return $contentService->publishVersion($draft->versionInfo);
109+
}
110+
111+
/**
112+
* @param string[] $languages
113+
*/
114+
protected function createUpdateStruct(Content $content, string $translatedName, array $languages): ContentUpdateStruct
115+
{
116+
$contentService = self::getContentService();
117+
118+
$updateStruct = $contentService->newContentUpdateStruct();
119+
$updateStruct->initialLanguageCode = $languages[0];
120+
121+
if ($translatedName === '') {
122+
$translatedNameOrg = $content->getName();
123+
} else {
124+
$translatedNameOrg = $translatedName;
125+
}
126+
127+
foreach ($languages as $language) {
128+
$translatedName = "[$language]" . $translatedNameOrg;
129+
130+
$updateStruct->setField('name', $translatedName, $language);
131+
}
132+
133+
return $updateStruct;
134+
}
135+
136+
protected function createContentDraft(Content $content, string $languageCode): Content
137+
{
138+
$contentLanguageService = self::getLanguageService();
139+
140+
$language = $contentLanguageService->loadLanguage($languageCode);
141+
142+
return self::getContentService()->createContentDraft($content->contentInfo, null, null, $language);
143+
}
144+
145+
protected function updateContent(Content $draft, ContentUpdateStruct $updateStruct): Content
146+
{
147+
return self::getContentService()->updateContent($draft->versionInfo, $updateStruct);
148+
}
149+
}

0 commit comments

Comments
 (0)