[BUGFIX] Show correct namespace in example block of CommaSeparatedValueProcessor
[Packages/TYPO3.CMS.git] / typo3 / sysext / frontend / Classes / DataProcessing / CommaSeparatedValueProcessor.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\CsvUtility;
18 use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
19 use TYPO3\CMS\Frontend\ContentObject\DataProcessorInterface;
20
21 /**
22 * This data processor will take field data formatted as a string, where each line, separated by line feed,
23 * represents a row. By default columns are separated by the delimiter character "comma ,",
24 * and can be enclosed by the character 'quotation mark "', like the default in a regular CSV file.
25 *
26 * An example of such a field is "bodytext" in the CType "table".
27 *
28 * The table data is transformed to a multi dimensional array, taking the delimiter and enclosure into account,
29 * before it is passed to the view.
30 *
31 * Example field data:
32 *
33 * This is row 1 column 1|This is row 1 column 2|This is row 1 column 3
34 * This is row 2 column 1|This is row 2 column 2|This is row 2 column 3
35 * This is row 3 column 1|This is row 3 column 2|This is row 3 column 3
36 *
37 * Example TypoScript configuration:
38 *
39 * 10 = TYPO3\CMS\Frontend\DataProcessing\CommaSeparatedValueProcessor
40 * 10 {
41 * if.isTrue.field = bodytext
42 * fieldName = bodytext
43 * fieldDelimiter = |
44 * fieldEnclosure =
45 * maximumColumns = 2
46 * as = table
47 * }
48 *
49 * whereas "table" can be used as a variable {table} inside Fluid for iteration.
50 *
51 * Using maximumColumns limits the amount of columns in the multi dimensional array.
52 * In the example, field data of the last column will be stripped off.
53 *
54 * Multi line cells are taken into account.
55 */
56 class CommaSeparatedValueProcessor implements DataProcessorInterface
57 {
58 /**
59 * Process CSV field data to split into a multi dimensional array
60 *
61 * @param ContentObjectRenderer $cObj The data of the content element or page
62 * @param array $contentObjectConfiguration The configuration of Content Object
63 * @param array $processorConfiguration The configuration of this processor
64 * @param array $processedData Key/value store of processed data (e.g. to be passed to a Fluid View)
65 * @return array the processed data as key/value store
66 */
67 public function process(ContentObjectRenderer $cObj, array $contentObjectConfiguration, array $processorConfiguration, array $processedData)
68 {
69 if (isset($processorConfiguration['if.']) && !$cObj->checkIf($processorConfiguration['if.'])) {
70 return $processedData;
71 }
72
73 // The field name to process
74 $fieldName = $cObj->stdWrapValue('fieldName', $processorConfiguration);
75 if (empty($fieldName)) {
76 return $processedData;
77 }
78
79 $originalValue = $cObj->data[$fieldName];
80
81 // Set the target variable
82 $targetVariableName = $cObj->stdWrapValue('as', $processorConfiguration, $fieldName);
83
84 // Set the maximum amount of columns
85 $maximumColumns = $cObj->stdWrapValue('maximumColumns', $processorConfiguration, 0);
86
87 // Set the field delimiter which is "," by default
88 $fieldDelimiter = $cObj->stdWrapValue('fieldDelimiter', $processorConfiguration, ',');
89
90 // Set the field enclosure which is " by default
91 $fieldEnclosure = $cObj->stdWrapValue('fieldEnclosure', $processorConfiguration, '"');
92
93 $processedData[$targetVariableName] = CsvUtility::csvToArray(
94 $originalValue,
95 $fieldDelimiter,
96 $fieldEnclosure,
97 (int)$maximumColumns
98 );
99
100 return $processedData;
101 }
102 }