@@ 16-67 (lines=52) @@ | ||
13 | /** |
|
14 | * A data mapper using property paths to read/write data. |
|
15 | */ |
|
16 | final class CreateContentMapper extends DataMapper |
|
17 | { |
|
18 | protected function mapToForm(FormInterface $form, DataWrapper $data, PropertyPathInterface $propertyPath): void |
|
19 | { |
|
20 | /** @var \eZ\Publish\Core\Repository\Values\Content\ContentCreateStruct $contentCreateStruct */ |
|
21 | $contentCreateStruct = $data->payload; |
|
22 | $contentType = $contentCreateStruct->contentType; |
|
23 | ||
24 | $fieldDefinitionIdentifier = (string) $propertyPath; |
|
25 | $fieldDefinition = $contentType->getFieldDefinition($fieldDefinitionIdentifier); |
|
26 | ||
27 | if (null === $fieldDefinition) { |
|
28 | throw new RuntimeException( |
|
29 | "Data payload does not contain expected FieldDefinition '{$fieldDefinitionIdentifier}'" |
|
30 | ); |
|
31 | } |
|
32 | ||
33 | $fieldTypeIdentifier = $fieldDefinition->fieldTypeIdentifier; |
|
34 | ||
35 | $handler = $this->fieldTypeHandlerRegistry->get($fieldTypeIdentifier); |
|
36 | $form->setData( |
|
37 | $handler->convertFieldValueToForm( |
|
38 | $contentType->getFieldDefinition($fieldDefinitionIdentifier)->defaultValue, |
|
39 | $fieldDefinition |
|
40 | ) |
|
41 | ); |
|
42 | } |
|
43 | ||
44 | protected function mapFromForm(FormInterface $form, DataWrapper $data, PropertyPathInterface $propertyPath): void |
|
45 | { |
|
46 | /** @var \eZ\Publish\Core\Repository\Values\Content\ContentCreateStruct $contentCreateStruct */ |
|
47 | $contentCreateStruct = $data->payload; |
|
48 | $contentType = $contentCreateStruct->contentType; |
|
49 | ||
50 | $fieldDefinitionIdentifier = (string) $propertyPath; |
|
51 | $fieldDefinition = $contentType->getFieldDefinition($fieldDefinitionIdentifier); |
|
52 | ||
53 | if (null === $fieldDefinition) { |
|
54 | throw new RuntimeException( |
|
55 | "Data payload does not contain expected FieldDefinition '{$fieldDefinitionIdentifier}'" |
|
56 | ); |
|
57 | } |
|
58 | ||
59 | $fieldTypeIdentifier = $fieldDefinition->fieldTypeIdentifier; |
|
60 | ||
61 | $handler = $this->fieldTypeHandlerRegistry->get($fieldTypeIdentifier); |
|
62 | $contentCreateStruct->setField( |
|
63 | $fieldDefinitionIdentifier, |
|
64 | $handler->convertFieldValueFromForm($form->getData()) |
|
65 | ); |
|
66 | } |
|
67 | } |
|
68 |
@@ 15-79 (lines=65) @@ | ||
12 | use Symfony\Component\Form\FormInterface; |
|
13 | use Symfony\Component\PropertyAccess\PropertyPathInterface; |
|
14 | ||
15 | final class InformationCollectionMapper extends DataMapper |
|
16 | { |
|
17 | /** |
|
18 | * Maps data from eZ Publish structure to the form. |
|
19 | */ |
|
20 | protected function mapToForm( |
|
21 | FormInterface $form, |
|
22 | DataWrapper $data, |
|
23 | PropertyPathInterface $propertyPath |
|
24 | ): void { |
|
25 | /** @var ContentType $contentType */ |
|
26 | $contentType = $data->definition; |
|
27 | ||
28 | $fieldDefinitionIdentifier = (string) $propertyPath; |
|
29 | $fieldDefinition = $contentType->getFieldDefinition($fieldDefinitionIdentifier); |
|
30 | ||
31 | if (null === $fieldDefinition) { |
|
32 | throw new RuntimeException( |
|
33 | "Data definition does not contain expected FieldDefinition '{$fieldDefinitionIdentifier}'" |
|
34 | ); |
|
35 | } |
|
36 | ||
37 | $fieldTypeIdentifier = $fieldDefinition->fieldTypeIdentifier; |
|
38 | ||
39 | $handler = $this->fieldTypeHandlerRegistry->get($fieldTypeIdentifier); |
|
40 | ||
41 | $form->setData( |
|
42 | $handler->convertFieldValueToForm( |
|
43 | $contentType->getFieldDefinition($fieldDefinitionIdentifier)->defaultValue, |
|
44 | $fieldDefinition |
|
45 | ) |
|
46 | ); |
|
47 | } |
|
48 | ||
49 | /** |
|
50 | * Maps data from form to the eZ Publish structure. |
|
51 | */ |
|
52 | protected function mapFromForm( |
|
53 | FormInterface $form, |
|
54 | DataWrapper $data, |
|
55 | PropertyPathInterface $propertyPath |
|
56 | ): void { |
|
57 | /** @var InformationCollectionStruct $payload */ |
|
58 | $payload = $data->payload; |
|
59 | /** @var ContentType $contentType */ |
|
60 | $contentType = $data->definition; |
|
61 | ||
62 | $fieldDefinitionIdentifier = (string) $propertyPath; |
|
63 | $fieldDefinition = $contentType->getFieldDefinition($fieldDefinitionIdentifier); |
|
64 | ||
65 | if (null === $fieldDefinition) { |
|
66 | throw new RuntimeException( |
|
67 | "Data definition does not contain expected FieldDefinition '{$fieldDefinitionIdentifier}'" |
|
68 | ); |
|
69 | } |
|
70 | ||
71 | $fieldTypeIdentifier = $fieldDefinition->fieldTypeIdentifier; |
|
72 | $handler = $this->fieldTypeHandlerRegistry->get($fieldTypeIdentifier); |
|
73 | ||
74 | $payload->setCollectedFieldValue( |
|
75 | $fieldDefinitionIdentifier, |
|
76 | $handler->convertFieldValueFromForm($form->getData()) |
|
77 | ); |
|
78 | } |
|
79 | } |
|
80 |