c66298b46b5072501cf5f8dae16173d79fe01806
[Packages/TYPO3.CMS.git] / typo3 / sysext / frontend / Classes / DataProcessing / FilesProcessor.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 use TYPO3\CMS\Frontend\Resource\FileCollector;
21
22 /**
23 * This data processor can be used for processing data for record which contain
24 * relations to sys_file records (e.g. sys_file_reference records) or for fetching
25 * files directly from UIDs or from folders or collections.
26 *
27 *
28 * Example TypoScript configuration:
29 *
30 * 10 = TYPO3\CMS\Frontend\DataProcessing\FilesProcessor
31 * 10 {
32 * references.fieldName = image
33 * collections = 13,15
34 * as = myfiles
35 * }
36 *
37 * whereas "myfiles" can further be used as a variable {myfiles} inside a Fluid template for iteration.
38 */
39 class FilesProcessor implements DataProcessorInterface
40 {
41 /**
42 * Process data of a record to resolve File objects to the view
43 *
44 * @param ContentObjectRenderer $cObj The data of the content element or page
45 * @param array $contentObjectConfiguration The configuration of Content Object
46 * @param array $processorConfiguration The configuration of this processor
47 * @param array $processedData Key/value store of processed data (e.g. to be passed to a Fluid View)
48 * @return array the processed data as key/value store
49 */
50 public function process(ContentObjectRenderer $cObj, array $contentObjectConfiguration, array $processorConfiguration, array $processedData)
51 {
52 if (isset($processorConfiguration['if.']) && !$cObj->checkIf($processorConfiguration['if.'])) {
53 return $processedData;
54 }
55
56 // gather data
57 /** @var FileCollector $fileCollector */
58 $fileCollector = GeneralUtility::makeInstance(FileCollector::class);
59
60 // references / relations
61 if (!empty($processorConfiguration['references.'])) {
62 $referenceConfiguration = $processorConfiguration['references.'];
63 $relationField = $cObj->stdWrapValue('fieldName', $referenceConfiguration);
64
65 // If no reference fieldName is set, there's nothing to do
66 if (!empty($relationField)) {
67 // Fetch the references of the default element
68 $relationTable = $cObj->stdWrapValue('table', $referenceConfiguration, $cObj->getCurrentTable());
69 if (!empty($relationTable)) {
70 $fileCollector->addFilesFromRelation($relationTable, $relationField, $cObj->data);
71 }
72 }
73 }
74
75 // files
76 $files = $cObj->stdWrapValue('files', $processorConfiguration);
77 if ($files) {
78 $files = GeneralUtility::intExplode(',', $files, true);
79 $fileCollector->addFiles($files);
80 }
81
82 // collections
83 $collections = $cObj->stdWrapValue('collections', $processorConfiguration);
84 if (!empty($collections)) {
85 $collections = GeneralUtility::trimExplode(',', $collections, true);
86 $fileCollector->addFilesFromFileCollections($collections);
87 }
88
89 // folders
90 $folders = $cObj->stdWrapValue('folders', $processorConfiguration);
91 if (!empty($folders)) {
92 $folders = GeneralUtility::trimExplode(',', $folders, true);
93 $fileCollector->addFilesFromFolders($folders, !empty($processorConfiguration['folders.']['recursive']));
94 }
95
96 // make sure to sort the files
97 $sortingProperty = $cObj->stdWrapValue('sorting', $processorConfiguration);
98 if ($sortingProperty) {
99 $sortingDirection = $cObj->stdWrapValue(
100 'direction',
101 isset($processorConfiguration['sorting.']) ? $processorConfiguration['sorting.'] : [],
102 'ascending'
103 );
104
105 $fileCollector->sort($sortingProperty, $sortingDirection);
106 }
107
108 // set the files into a variable, default "files"
109 $targetVariableName = $cObj->stdWrapValue('as', $processorConfiguration, 'files');
110 $processedData[$targetVariableName] = $fileCollector->getFiles();
111
112 return $processedData;
113 }
114 }