Exponential 5.x Kernel 7.5.40 Exponential New Stack (Symfony-based) Kernel — PHP Class Reference

AddField.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Publish\Core\Persistence\Legacy\Content\Type\ContentUpdater\Action;
8
9 use eZ\Publish\Core\Persistence\Legacy\Content\Type\ContentUpdater\Action;
10 use eZ\Publish\SPI\Persistence\Content;
11 use eZ\Publish\SPI\Persistence\Content\Field;
12 use eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter;
13 use eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue;
14 use eZ\Publish\Core\Persistence\Legacy\Content\Gateway;
15 use eZ\Publish\Core\Persistence\Legacy\Content\StorageHandler;
16 use eZ\Publish\Core\Persistence\Legacy\Content\Mapper as ContentMapper;
17 use eZ\Publish\SPI\Persistence\Content\Type\FieldDefinition;
18
22 class AddField extends Action
23 {
29 protected $fieldDefinition;
30
36 protected $storageHandler;
37
43 protected $fieldValueConverter;
44
46 protected $contentMapper;
47
57 public function __construct(
58 Gateway $contentGateway,
59 FieldDefinition $fieldDef,
60 Converter $converter,
61 StorageHandler $storageHandler,
62 ContentMapper $contentMapper
63 ) {
64 $this->contentGateway = $contentGateway;
65 $this->fieldDefinition = $fieldDef;
66 $this->fieldValueConverter = $converter;
67 $this->storageHandler = $storageHandler;
68 $this->contentMapper = $contentMapper;
69 }
70
76 public function apply($contentId)
77 {
78 $versionNumbers = $this->contentGateway->listVersionNumbers($contentId);
79 $languageCodeToFieldId = [];
80
81 $nameRows = $this->contentGateway->loadVersionedNameData(
82 array_map(
83 function ($versionNo) use ($contentId) {
84 return ['id' => $contentId, 'version' => $versionNo];
85 },
86 $versionNumbers
87 )
88 );
89
90 foreach ($versionNumbers as $versionNo) {
91 $contentRows = $this->contentGateway->load($contentId, $versionNo);
92 $contentList = $this->contentMapper->extractContentFromRows($contentRows, $nameRows);
93 $content = $contentList[0];
94 $languageCodeSet = [];
95
96 // Each subsequent Content version can have additional language(s)
97 foreach ($content->fields as $field) {
98 $languageCode = $field->languageCode;
99
100 // Add once for each language per version
101 if (isset($languageCodeSet[$languageCode])) {
102 continue;
103 }
104
105 $languageCodeSet[$languageCode] = true;
106
107 // Check if field was already inserted for current language code,
108 // in that case we need to preserve its ID across versions
109 if (isset($languageCodeToFieldId[$languageCode])) {
110 $fieldId = $languageCodeToFieldId[$languageCode];
111 } else {
112 $fieldId = null;
113 }
114
115 $languageCodeToFieldId[$languageCode] = $this->insertField(
116 $content,
117 $this->createField(
118 $fieldId,
119 $versionNo,
120 $languageCode
121 )
122 );
123 }
124 }
125 }
126
138 protected function insertField(Content $content, Field $field)
139 {
140 $storageValue = new StorageFieldValue();
141 $this->fieldValueConverter->toStorageValue(
142 $field->value,
143 $storageValue
144 );
145
146 if (isset($field->id)) {
147 // Insert with existing Field id and given Content version number
148 $this->contentGateway->insertExistingField(
149 $content,
150 $field,
151 $storageValue
152 );
153 } else {
154 // Insert with creating new Field id and given Content version number
155 $field->id = $this->contentGateway->insertNewField(
156 $content,
157 $field,
158 $storageValue
159 );
160 }
161
162 // If the storage handler returns true, it means that $field value has been modified
163 // So we need to update it in order to store those modifications
164 // Field converter is called once again via the Mapper
165 if ($this->storageHandler->storeFieldData($content->versionInfo, $field) === true) {
166 $storageValue = new StorageFieldValue();
167 $this->fieldValueConverter->toStorageValue(
168 $field->value,
169 $storageValue
170 );
171
172 $this->contentGateway->updateField(
173 $field,
174 $storageValue
175 );
176 }
177
178 return $field->id;
179 }
180
191 protected function createField($id, $versionNo, $languageCode)
192 {
193 $field = new Field();
194
195 $field->id = $id;
196 $field->fieldDefinitionId = $this->fieldDefinition->id;
197 $field->type = $this->fieldDefinition->fieldType;
198 $field->value = clone $this->fieldDefinition->defaultValue;
199 $field->versionNo = $versionNo;
200 $field->languageCode = $languageCode;
201
202 return $field;
203 }
204 }
eZ\Publish\Core\Persistence\Legacy\Content\Gateway
Base class for content gateways.
Definition: Persistence/Legacy/Content/Gateway.php:21
eZ\Publish\Core\Persistence\Legacy\Content\Mapper
Mapper for Content Handler.
Definition: Publish/Core/Persistence/Legacy/Content/Mapper.php:27
eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue
Definition: StorageFieldValue.php:12
eZ\Publish\Core\Persistence\Legacy\Content\StorageHandler
Handler for external storages.
Definition: StorageHandler.php:16
eZ\Publish\Core\Persistence\Legacy\Content\Type\ContentUpdater\Action\AddField
Action to add a field to content objects.
Definition: AddField.php:23
eZ\Publish\Core\Persistence\Legacy\Content\Type\ContentUpdater\Action\AddField\insertField
insertField(Content $content, Field $field)
Inserts given $field to the internal and external storage.
Definition: AddField.php:138
eZ\Publish\Core\Persistence\Legacy\Content\Type\ContentUpdater\Action\AddField\$fieldValueConverter
$fieldValueConverter
Definition: AddField.php:43
eZ\Publish\Core\Persistence\Legacy\Content\Type\ContentUpdater\Action\AddField\__construct
__construct(Gateway $contentGateway, FieldDefinition $fieldDef, Converter $converter, StorageHandler $storageHandler, ContentMapper $contentMapper)
Creates a new action.
Definition: AddField.php:57
eZ\Publish\Core\Persistence\Legacy\Content\Type\ContentUpdater\Action\AddField\$storageHandler
$storageHandler
Definition: AddField.php:36
eZ\Publish\Core\Persistence\Legacy\Content\Type\ContentUpdater\Action\AddField\createField
createField($id, $versionNo, $languageCode)
Creates new Field value object, setting given parameters and default value for a field definition the...
Definition: AddField.php:191
eZ\Publish\Core\Persistence\Legacy\Content\Type\ContentUpdater\Action\AddField\apply
apply($contentId)
Applies the action to the given $content.
Definition: AddField.php:76
eZ\Publish\Core\Persistence\Legacy\Content\Type\ContentUpdater\Action\AddField\$fieldDefinition
$fieldDefinition
Definition: AddField.php:29
eZ\Publish\Core\Persistence\Legacy\Content\Type\ContentUpdater\Action\AddField\$contentMapper
$contentMapper
Definition: AddField.php:46
eZ\Publish\Core\Persistence\Legacy\Content\Type\ContentUpdater\Action
Updater action base class.
Definition: Action.php:15
eZ\Publish\Core\Persistence\Legacy\Content\Type\ContentUpdater\Action\$contentGateway
$contentGateway
Definition: Action.php:21
eZ\Publish\SPI\Persistence\Content\Field
Definition: SPI/Persistence/Content/Field.php:12
eZ\Publish\SPI\Persistence\Content\Type\FieldDefinition
Definition: SPI/Persistence/Content/Type/FieldDefinition.php:18
eZ\Publish\SPI\Persistence\Content\Type\$id
$id
Definition: SPI/Persistence/Content/Type.php:30
eZ\Publish\SPI\Persistence\Content
Content value object, bound to a version.
Definition: Publish/SPI/Persistence/Content.php:17
eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter
Converter for field values in legacy storage.
Definition: Persistence/Legacy/Content/FieldValue/Converter.php:18
eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter
Definition: AuthorConverter.php:7
eZ\Publish\Core\Persistence\Legacy\Content\Type\ContentUpdater\Action
Definition: AddField.php:7
eZ\Publish\SPI\Persistence\Content
Definition: SPI/Persistence/Content/ContentInfo.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.