FlexFormProcessor::process()   B
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 8.439
c 0
b 0
f 0
cc 5
eloc 16
nc 4
nop 4
1
<?php
2
namespace Bo\Slickcarousel\DataProcessing;
3
4
/*
5
 *  The MIT License (MIT)
6
 *
7
 *  Copyright (c) 2015 Benjamin Kott, http://www.bk2k.info
8
 *
9
 *  Permission is hereby granted, free of charge, to any person obtaining a copy
10
 *  of this software and associated documentation files (the "Software"), to deal
11
 *  in the Software without restriction, including without limitation the rights
12
 *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
 *  copies of the Software, and to permit persons to whom the Software is
14
 *  furnished to do so, subject to the following conditions:
15
 *
16
 *  The above copyright notice and this permission notice shall be included in
17
 *  all copies or substantial portions of the Software.
18
 *
19
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
 *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25
 *  THE SOFTWARE.
26
 */
27
28
use TYPO3\CMS\Core\Utility\GeneralUtility;
29
use TYPO3\CMS\Extbase\Service\FlexFormService;
30
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
31
use TYPO3\CMS\Frontend\ContentObject\DataProcessorInterface;
32
33
/**
34
 * Minimal TypoScript configuration
35
 * Process field pi_flexform and overrides the values stored in data
36
 *
37
 * 10 = Bo\Slickcarousel\DataProcessing\FlexFormProcessor
38
 *
39
 *
40
 * Advanced TypoScript configuration
41
 * Process field assigned in fieldName and stores processed data to new key
42
 *
43
 * 10 = Bo\Slickcarousel\DataProcessing\FlexFormProcessor
44
 * 10 {
45
 *   fieldName = pi_flexform
46
 *   as = flexform
47
 * }
48
 */
49
class FlexFormProcessor implements DataProcessorInterface
50
{
51
    /**
52
     * @var FlexFormService
53
     */
54
    protected $flexFormService;
55
56
    /**
57
     * Constructor
58
     */
59
    public function __construct()
60
    {
61
        $this->flexFormService = GeneralUtility::makeInstance(FlexFormService::class);
62
    }
63
64
    /**
65
     * @param ContentObjectRenderer $cObj The data of the content element or page
66
     * @param array $contentObjectConfiguration The configuration of Content Object
67
     * @param array $processorConfiguration The configuration of this processor
68
     * @param array $processedData Key/value store of processed data (e.g. to be passed to a Fluid View)
69
     * @return array the processed data as key/value store
70
     */
71
    public function process(ContentObjectRenderer $cObj, array $contentObjectConfiguration, array $processorConfiguration, array $processedData)
72
    {
73
        // The field name to process
74
        $fieldName = $cObj->stdWrapValue('fieldName', $processorConfiguration);
75
        if (empty($fieldName) && !$processedData['data']['pi_flexform']) {
76
            return $processedData;
77
        } else {
78
            $fieldName = 'pi_flexform';
79
        }
80
81
        // Process Flexform
82
        $originalValue = $processedData['data'][$fieldName];
83
        if (!is_string($originalValue)) {
84
            return $processedData;
85
        }
86
        $flexformData = $this->flexFormService->convertFlexFormContentToArray($originalValue);
87
88
        // Set the target variable
89
        $targetVariableName = $cObj->stdWrapValue('as', $processorConfiguration);
90
        if (!empty($targetVariableName)) {
91
            $processedData[$targetVariableName] = $flexformData;
92
        } else {
93
            $processedData['data'][$fieldName] = $flexformData;
94
        }
95
96
        return $processedData;
97
    }
98
}
99