Revert "[FEATURE] EXT:form - introduce YAML "imports""
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Classes / Property / TypeConverter / FormDefinitionArrayConverter.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Form\Property\TypeConverter;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18 use TYPO3\CMS\Core\Utility\ArrayUtility;
19 use TYPO3\CMS\Extbase\Property\Exception as PropertyException;
20 use TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface;
21 use TYPO3\CMS\Extbase\Property\TypeConverter\AbstractTypeConverter;
22 use TYPO3\CMS\Form\Type\FormDefinitionArray;
23
24 /**
25 * Converter for form definition arrays
26 *
27 * @internal
28 */
29 class FormDefinitionArrayConverter extends AbstractTypeConverter
30 {
31 /**
32 * @var array<string>
33 */
34 protected $sourceTypes = ['string'];
35
36 /**
37 * @var string
38 */
39 protected $targetType = FormDefinitionArray::class;
40
41 /**
42 * @var int
43 */
44 protected $priority = 10;
45
46 /**
47 * Convert from $source to $targetType, a noop if the source is an array.
48 * If it is an empty string it will be converted to an empty array.
49 *
50 * @param string $source
51 * @param string $targetType
52 * @param array $convertedChildProperties
53 * @param PropertyMappingConfigurationInterface $configuration
54 * @return FormDefinitionArray
55 * @throws PropertyException
56 */
57 public function convertFrom($source, $targetType, array $convertedChildProperties = [], PropertyMappingConfigurationInterface $configuration = null)
58 {
59 $rawFormDefinitionArray = json_decode($source, true);
60
61 if (json_last_error() !== JSON_ERROR_NONE) {
62 throw new PropertyException('Unable to decode JSON source: ' . json_last_error_msg(), 1512578002);
63 }
64
65 $rawFormDefinitionArray = ArrayUtility::stripTagsFromValuesRecursive($rawFormDefinitionArray);
66 $rawFormDefinitionArray = $this->convertJsonArrayToAssociativeArray($rawFormDefinitionArray);
67 $formDefinitionArray = new FormDefinitionArray($rawFormDefinitionArray);
68
69 return $formDefinitionArray;
70 }
71
72 /**
73 * Some data which is build by the form editor needs a transformation before
74 * it can be used by the framework.
75 * Multivalue elements like select elements produce data like:
76 *
77 * [
78 * _label => 'label'
79 * _value => 'value'
80 * ]
81 *
82 * This method transform this into:
83 *
84 * [
85 * 'value' => 'label'
86 * ]
87 *
88 * @param array $input
89 * @return array
90 */
91 protected function convertJsonArrayToAssociativeArray(array $input): array
92 {
93 $output = [];
94
95 foreach ($input as $key => $value) {
96 if (is_int($key) && is_array($value) && isset($value['_label']) && isset($value['_value'])) {
97 $key = $value['_value'];
98 $value = $value['_label'];
99 }
100
101 if (is_array($value)) {
102 $output[$key] = $this->convertJsonArrayToAssociativeArray($value);
103 } else {
104 $output[$key] = $value;
105 }
106 }
107
108 return $output;
109 }
110 }