[CLEANUP] Fix namespace in SplitProcessor example
[Packages/TYPO3.CMS.git] / typo3 / sysext / frontend / Classes / DataProcessing / SplitProcessor.php
1 <?php
2 namespace TYPO3\CMS\Frontend\DataProcessing;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Utility\GeneralUtility;
18 use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
19 use TYPO3\CMS\Frontend\ContentObject\DataProcessorInterface;
20
21 /**
22 * This data processor can be used for processing data for the content elements which have split contents in one field
23 * like e.g. "bullets". It will split the field data in an array ready to be iterated over in Fluid.
24 *
25 * Example field data:
26 *
27 * This is bullet 1, This is bullet 2, This is bullet 3
28 *
29 * Example TypoScript configuration:
30 *
31 * 10 = TYPO3\CMS\Frontend\DataProcessing\SplitProcessor
32 * 10 {
33 * if.isTrue.field = bodytext
34 * delimiter = ,
35 * fieldName = bodytext
36 * removeEmptyEntries = 1
37 * filterIntegers = 1
38 * filterUnique = 1
39 * as = bullets
40 * }
41 *
42 * whereas "bullets" can be used as a variable {bullets} inside Fluid for iteration.
43 */
44 class SplitProcessor implements DataProcessorInterface {
45
46 /**
47 * Process field data to split in an array
48 *
49 * @param ContentObjectRenderer $cObj The data of the content element or page
50 * @param array $contentObjectConfiguration The configuration of Content Object
51 * @param array $processorConfiguration The configuration of this processor
52 * @param array $processedData Key/value store of processed data (e.g. to be passed to a Fluid View)
53 * @return array the processed data as key/value store
54 */
55 public function process(ContentObjectRenderer $cObj, array $contentObjectConfiguration, array $processorConfiguration, array $processedData) {
56 if (isset($processorConfiguration['if.']) && !$cObj->checkIf($processorConfiguration['if.'])) {
57 return $processedData;
58 }
59
60 // The field name to process
61 $fieldName = $cObj->stdWrapValue('fieldName', $processorConfiguration);
62 if (empty($fieldName)) {
63 return $processedData;
64 }
65
66 $originalValue = $cObj->data[$fieldName];
67
68 // Set the target variable
69 $targetVariableName = $cObj->stdWrapValue('as', $processorConfiguration, $fieldName);
70
71 // Set the delimiter which is "LF" by default
72 $delimiter = $cObj->stdWrapValue('delimiter', $processorConfiguration, LF);
73
74 // Filter integers
75 $filterIntegers = (bool)$cObj->stdWrapValue('filterIntegers', $processorConfiguration, FALSE);
76
77 // Filter unique
78 $filterUnique = (bool)$cObj->stdWrapValue('filterUnique', $processorConfiguration, FALSE);
79
80 // Remove empty entries
81 $removeEmptyEntries = (bool)$cObj->stdWrapValue('removeEmptyEntries', $processorConfiguration, FALSE);
82
83 if ($filterIntegers === TRUE) {
84 $processedData[$targetVariableName] = GeneralUtility::intExplode($delimiter, $originalValue, $removeEmptyEntries);
85 } else {
86 $processedData[$targetVariableName] = GeneralUtility::trimExplode($delimiter, $originalValue, $removeEmptyEntries);
87 }
88
89 if ($filterUnique === TRUE) {
90 $processedData[$targetVariableName] = array_unique($processedData[$targetVariableName]);
91 }
92
93 return $processedData;
94 }
95 }