Passed
Push — master ( e4d3e3...1d2b3b )
by
unknown
16:29
created

FlexFormProcessor::process()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 32
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 32
rs 9.5222
cc 5
nc 4
nop 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace TYPO3\CMS\Frontend\DataProcessing;
6
7
/*
8
 * This file is part of the TYPO3 CMS project.
9
 *
10
 * It is free software; you can redistribute it and/or modify it under
11
 * the terms of the GNU General Public License, either version 2
12
 * of the License, or any later version.
13
 *
14
 * For the full copyright and license information, please read the
15
 * LICENSE.txt file that was distributed with this source code.
16
 *
17
 * The TYPO3 project - inspiring people to share!
18
 */
19
20
use TYPO3\CMS\Core\Service\FlexFormService;
21
use TYPO3\CMS\Core\Utility\GeneralUtility;
22
use TYPO3\CMS\Frontend\ContentObject\ContentDataProcessor;
23
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
24
use TYPO3\CMS\Frontend\ContentObject\DataProcessorInterface;
25
26
/**
27
 * This data processor converts the XML structure of a given FlexForm field
28
 * into a fluid readable array.
29
 *
30
 * Options:
31
 * fieldname - The name of the field containing the FlexForm to be converted
32
 * as        - The variable, the generated array should be assigned to
33
 *
34
 * Example of a minimal TypoScript configuration, which processes the field
35
 * `pi_flexform` and assigns the array to the `flexFormData` variable:
36
 *
37
 * 10 = TYPO3\CMS\Frontend\DataProcessing\FlexFormProcessor
38
 *
39
 * Example of an advanced TypoScript configuration, which processes the field
40
 * `my_flexform_field` and assigns the array to the `myOutputVariable` variable:
41
 *
42
 * 10 = TYPO3\CMS\Frontend\DataProcessing\FlexFormProcessor
43
 * 10 {
44
 *   fieldName = my_flexform_field
45
 *   as = myOutputVariable
46
 * }
47
 */
48
class FlexFormProcessor implements DataProcessorInterface
49
{
50
    /**
51
     * @param ContentObjectRenderer $cObj The data of the content element or page
52
     * @param array $contentObjectConfiguration The configuration of Content Object
53
     * @param array $processorConfiguration The configuration of this processor
54
     * @param array $processedData Key/value store of processed data (e.g. to be passed to a Fluid View)
55
     * @return array the processed data as key/value store
56
     */
57
    public function process(
58
        ContentObjectRenderer $cObj,
59
        array $contentObjectConfiguration,
60
        array $processorConfiguration,
61
        array $processedData
62
    ): array {
63
64
        // The field name to process
65
        $fieldName = $cObj->stdWrapValue('fieldName', $processorConfiguration, 'pi_flexform');
66
67
        if (!isset($processedData['data'][$fieldName])) {
68
            return $processedData;
69
        }
70
71
        // Process FlexForm
72
        $originalValue = $processedData['data'][$fieldName];
73
        if (!is_string($originalValue)) {
74
            return $processedData;
75
        }
76
        $flexFormData = GeneralUtility::makeInstance(FlexFormService::class)
77
            ->convertFlexFormContentToArray($originalValue);
78
79
        // Set the target variable
80
        $targetVariableName = $cObj->stdWrapValue('as', $processorConfiguration, 'flexFormData');
81
82
        if (isset($processorConfiguration['dataProcessing.']) && is_array($processorConfiguration['dataProcessing.'])) {
83
            $flexFormData = $this->processAdditionalDataProcessors($flexFormData, $processorConfiguration);
84
        }
85
86
        $processedData[$targetVariableName] = $flexFormData;
87
88
        return $processedData;
89
    }
90
91
    /**
92
     * Recursively process sub processors of a data processor
93
     *
94
     * @param array $data
95
     * @param array $processorConfiguration
96
     * @return array
97
     */
98
    public function processAdditionalDataProcessors(array $data, array $processorConfiguration): array
99
    {
100
        $contentObjectRenderer = GeneralUtility::makeInstance(ContentObjectRenderer::class);
101
        $contentObjectRenderer->start([$data]);
102
        return GeneralUtility::makeInstance(ContentDataProcessor::class)->process(
103
            $contentObjectRenderer,
104
            $processorConfiguration,
105
            $data
106
        );
107
    }
108
}
109