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

RelationConverter.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter;
8
9 use DOMDocument;
10 use eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter;
11 use eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue;
12 use eZ\Publish\SPI\Persistence\Content\FieldValue;
13 use eZ\Publish\SPI\Persistence\Content\Type\FieldDefinition;
14 use eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldDefinition;
15
16 class RelationConverter implements Converter
17 {
27 public static function create()
28 {
29 return new self();
30 }
31
38 public function toStorageValue(FieldValue $value, StorageFieldValue $storageFieldValue)
39 {
40 $storageFieldValue->dataInt = !empty($value->data['destinationContentId'])
41 ? $value->data['destinationContentId']
42 : null;
43 $storageFieldValue->sortKeyInt = (int)$value->sortKey;
44 }
45
52 public function toFieldValue(StorageFieldValue $value, FieldValue $fieldValue)
53 {
54 $fieldValue->data = [
55 'destinationContentId' => $value->dataInt ?: null,
56 ];
57 $fieldValue->sortKey = (int)$value->sortKeyInt;
58 }
59
66 public function toStorageFieldDefinition(FieldDefinition $fieldDef, StorageFieldDefinition $storageDef)
67 {
68 $fieldSettings = $fieldDef->fieldTypeConstraints->fieldSettings;
69 $doc = new DOMDocument('1.0', 'utf-8');
70 $root = $doc->createElement('related-objects');
71 $doc->appendChild($root);
72
73 $constraints = $doc->createElement('constraints');
74 if (!empty($fieldSettings['selectionContentTypes'])) {
75 foreach ($fieldSettings['selectionContentTypes'] as $typeIdentifier) {
76 $allowedClass = $doc->createElement('allowed-class');
77 $allowedClass->setAttribute('contentclass-identifier', $typeIdentifier);
78 $constraints->appendChild($allowedClass);
79 unset($allowedClass);
80 }
81 }
82 $root->appendChild($constraints);
83
84 $selectionType = $doc->createElement('selection_type');
85 $selectionMethod = isset($fieldSettings['selectionMethod']) ? (int)$fieldSettings['selectionMethod'] : 0;
86 $selectionType->setAttribute('value', $selectionMethod);
87 $root->appendChild($selectionType);
88
89 $defaultLocation = $doc->createElement('contentobject-placement');
90 if (!empty($fieldSettings['selectionRoot'])) {
91 $defaultLocation->setAttribute('node-id', (int)$fieldSettings['selectionRoot']);
92 }
93 $root->appendChild($defaultLocation);
94
95 $doc->appendChild($root);
96 $storageDef->dataText5 = $doc->saveXML();
97
98 // BC: For Backwards Compatibility for legacy and in case of downgrades or data sharing
99 // Selection method, 0 = browse, 1 = dropdown
100 $storageDef->dataInt1 = $selectionMethod;
101
102 // Selection root, location ID, or 0 if empty
103 $storageDef->dataInt2 = (int)$fieldSettings['selectionRoot'];
104 }
105
130 public function toFieldDefinition(StorageFieldDefinition $storageDef, FieldDefinition $fieldDef)
131 {
132 // default settings
133 // use dataInt1 and dataInt2 fields as default for backward compatibility
134 $fieldDef->fieldTypeConstraints->fieldSettings = [
135 'selectionMethod' => $storageDef->dataInt1,
136 'selectionRoot' => $storageDef->dataInt2 === 0 ? '' : $storageDef->dataInt2,
137 'selectionContentTypes' => [],
138 ];
139
140 if ($storageDef->dataText5 === null) {
141 return;
142 }
143
144 // read settings from storage
145 $fieldSettings = &$fieldDef->fieldTypeConstraints->fieldSettings;
146 $dom = new DOMDocument('1.0', 'utf-8');
147 if (empty($storageDef->dataText5) || $dom->loadXML($storageDef->dataText5) !== true) {
148 return;
149 }
150
151 if (
152 ($selectionType = $dom->getElementsByTagName('selection_type')->item(0)) &&
153 $selectionType->hasAttribute('value')
154 ) {
155 $fieldSettings['selectionMethod'] = (int)$selectionType->getAttribute('value');
156 }
157
158 if (
159 ($defaultLocation = $dom->getElementsByTagName('contentobject-placement')->item(0)) &&
160 $defaultLocation->hasAttribute('node-id')
161 ) {
162 $fieldSettings['selectionRoot'] = (int)$defaultLocation->getAttribute('node-id');
163 }
164
165 if (!($constraints = $dom->getElementsByTagName('constraints'))) {
166 return;
167 }
168
169 foreach ($constraints->item(0)->getElementsByTagName('allowed-class') as $allowedClass) {
170 $fieldSettings['selectionContentTypes'][] = $allowedClass->getAttribute('contentclass-identifier');
171 }
172 }
173
183 public function getIndexColumn()
184 {
185 return 'sort_key_int';
186 }
187 }
eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\RelationConverter
Definition: RelationConverter.php:17
eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\RelationConverter\toFieldDefinition
toFieldDefinition(StorageFieldDefinition $storageDef, FieldDefinition $fieldDef)
Converts field definition data in $storageDef into $fieldDef.
Definition: RelationConverter.php:130
eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\RelationConverter\getIndexColumn
getIndexColumn()
Returns the name of the index column in the attribute table.
Definition: RelationConverter.php:183
eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\RelationConverter\toStorageValue
toStorageValue(FieldValue $value, StorageFieldValue $storageFieldValue)
Converts data from $value to $storageFieldValue.
Definition: RelationConverter.php:38
eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\RelationConverter\toFieldValue
toFieldValue(StorageFieldValue $value, FieldValue $fieldValue)
Converts data from $value to $fieldValue.
Definition: RelationConverter.php:52
eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\RelationConverter\toStorageFieldDefinition
toStorageFieldDefinition(FieldDefinition $fieldDef, StorageFieldDefinition $storageDef)
Converts field definition data in $fieldDef into $storageFieldDef.
Definition: RelationConverter.php:66
eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\RelationConverter\create
static create()
Factory for current class.
Definition: RelationConverter.php:27
eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldDefinition
Definition: StorageFieldDefinition.php:12
eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue
Definition: StorageFieldValue.php:12
eZ\Publish\SPI\Persistence\Content\FieldValue
Definition: FieldValue.php:12
eZ\Publish\SPI\Persistence\Content\Type\FieldDefinition
Definition: SPI/Persistence/Content/Type/FieldDefinition.php:18
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
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.