-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathContentUpdateMapper.php
More file actions
73 lines (62 loc) · 2.91 KB
/
Copy pathContentUpdateMapper.php
File metadata and controls
73 lines (62 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);
namespace Ibexa\ContentForms\Data\Mapper;
use Ibexa\ContentForms\Data\Content\ContentUpdateData;
use Ibexa\Contracts\ContentForms\Data\Content\FieldData;
use Ibexa\Contracts\Core\Repository\Values\ContentType\ContentType;
use Ibexa\Contracts\Core\Repository\Values\ValueObject;
use Symfony\Component\OptionsResolver\OptionsResolver;
class ContentUpdateMapper implements FormDataMapperInterface
{
/**
* Maps a ValueObject from Ibexa content repository to a data usable as underlying form data (e.g. create/update struct).
*
* @param \Ibexa\Contracts\Core\Repository\Values\Content\Content|\Ibexa\Contracts\Core\Repository\Values\ValueObject $contentDraft
* @param array $params
*
* @return \Ibexa\ContentForms\Data\Content\ContentUpdateData
*/
public function mapToFormData(ValueObject $contentDraft, array $params = [])
{
$optionsResolver = new OptionsResolver();
$this->configureOptions($optionsResolver);
$params = $optionsResolver->resolve($params);
$languageCode = $params['languageCode'];
$currentFields = $params['currentFields'];
$mappedCurrentFields = array_column($currentFields, null, 'fieldDefIdentifier');
$data = new ContentUpdateData(['contentDraft' => $contentDraft]);
$data->initialLanguageCode = $languageCode;
$fields = $contentDraft->getFieldsByLanguage($languageCode);
$mainLanguageCode = $contentDraft->getVersionInfo()->getContentInfo()->getMainLanguage()->getLanguageCode();
foreach ($params['contentType']->fieldDefinitions as $fieldDef) {
$isNonTranslatable = $fieldDef->isTranslatable === false;
if (!isset($fields[$fieldDef->identifier])) {
continue;
}
$field = $fields[$fieldDef->identifier];
$shouldUseCurrentFieldValue = $isNonTranslatable
&& isset($mappedCurrentFields[$fieldDef->identifier])
&& $mainLanguageCode !== $languageCode;
$data->addFieldData(new FieldData([
'fieldDefinition' => $fieldDef,
'field' => $field,
'value' => $shouldUseCurrentFieldValue
? $mappedCurrentFields[$fieldDef->identifier]->value
: $field->value,
]));
}
return $data;
}
private function configureOptions(OptionsResolver $optionsResolver)
{
$optionsResolver
->setRequired(['languageCode', 'contentType', 'currentFields'])
->setAllowedTypes('contentType', ContentType::class)
->setDefault('currentFields', []);
}
}
class_alias(ContentUpdateMapper::class, 'EzSystems\EzPlatformContentForms\Data\Mapper\ContentUpdateMapper');