|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Netgen\Bundle\EzFormsBundle\Form\DataMapper; |
|
6
|
|
|
|
|
7
|
|
|
use Netgen\Bundle\EzFormsBundle\Form\DataMapper; |
|
8
|
|
|
use Netgen\Bundle\EzFormsBundle\Form\DataWrapper; |
|
9
|
|
|
use RuntimeException; |
|
10
|
|
|
use Symfony\Component\Form\FormInterface; |
|
11
|
|
|
use Symfony\Component\PropertyAccess\PropertyPathInterface; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* A data mapper using property paths to read/write data. |
|
15
|
|
|
*/ |
|
16
|
|
|
final class UpdateContentMapper extends DataMapper |
|
17
|
|
|
{ |
|
18
|
|
|
protected function mapToForm(FormInterface $form, DataWrapper $data, PropertyPathInterface $propertyPath): void |
|
19
|
|
|
{ |
|
20
|
|
|
/** @var \eZ\Publish\API\Repository\Values\Content\ContentUpdateStruct $contentUpdateStruct */ |
|
21
|
|
|
$contentUpdateStruct = $data->payload; |
|
22
|
|
|
/** @var \eZ\Publish\API\Repository\Values\Content\Content $content */ |
|
23
|
|
|
$content = $data->target; |
|
24
|
|
|
/** @var \eZ\Publish\API\Repository\Values\ContentType\ContentType $contentType */ |
|
25
|
|
|
$contentType = $data->definition; |
|
26
|
|
|
|
|
27
|
|
|
$fieldDefinitionIdentifier = (string) $propertyPath; |
|
28
|
|
|
$fieldDefinition = $contentType->getFieldDefinition($fieldDefinitionIdentifier); |
|
29
|
|
|
|
|
30
|
|
|
if (null === $fieldDefinition) { |
|
31
|
|
|
throw new RuntimeException( |
|
32
|
|
|
"Data payload does not contain expected FieldDefinition '{$fieldDefinitionIdentifier}'" |
|
33
|
|
|
); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
$fieldTypeIdentifier = $fieldDefinition->fieldTypeIdentifier; |
|
37
|
|
|
|
|
38
|
|
|
$handler = $this->fieldTypeHandlerRegistry->get($fieldTypeIdentifier); |
|
39
|
|
|
$form->setData( |
|
40
|
|
|
$handler->convertFieldValueToForm( |
|
41
|
|
|
$content->getFieldValue( |
|
|
|
|
|
|
42
|
|
|
$fieldDefinitionIdentifier, |
|
43
|
|
|
$contentUpdateStruct->initialLanguageCode |
|
44
|
|
|
), |
|
45
|
|
|
$fieldDefinition |
|
46
|
|
|
) |
|
47
|
|
|
); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
protected function mapFromForm(FormInterface $form, DataWrapper $data, PropertyPathInterface $propertyPath): void |
|
51
|
|
|
{ |
|
52
|
|
|
/** @var \eZ\Publish\API\Repository\Values\Content\ContentUpdateStruct $contentUpdateStruct */ |
|
53
|
|
|
$contentUpdateStruct = $data->payload; |
|
54
|
|
|
/** @var \eZ\Publish\API\Repository\Values\ContentType\ContentType $contentType */ |
|
55
|
|
|
$contentType = $data->definition; |
|
56
|
|
|
|
|
57
|
|
|
$fieldDefinitionIdentifier = (string) $propertyPath; |
|
58
|
|
|
$fieldDefinition = $contentType->getFieldDefinition($fieldDefinitionIdentifier); |
|
59
|
|
|
|
|
60
|
|
|
if (null === $fieldDefinition) { |
|
61
|
|
|
throw new RuntimeException( |
|
62
|
|
|
"Data payload does not contain expected FieldDefinition '{$fieldDefinitionIdentifier}'" |
|
63
|
|
|
); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$fieldTypeIdentifier = $fieldDefinition->fieldTypeIdentifier; |
|
67
|
|
|
$formData = $form->getData(); |
|
68
|
|
|
|
|
69
|
|
|
// Set field to struct only if it is not empty or it has not been marked |
|
70
|
|
|
// to skip update if empty |
|
71
|
|
|
if (!$this->shouldSkipForEmptyUpdate($form, $formData, $fieldDefinitionIdentifier)) { |
|
72
|
|
|
$handler = $this->fieldTypeHandlerRegistry->get($fieldTypeIdentifier); |
|
73
|
|
|
$contentUpdateStruct->setField( |
|
74
|
|
|
$fieldDefinitionIdentifier, |
|
75
|
|
|
$handler->convertFieldValueFromForm($form->getData()) |
|
76
|
|
|
); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: