‪TYPO3CMS  10.4
FilesContentObject.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
22 
27 {
34  public function ‪render($conf = [])
35  {
36  if (!empty($conf['if.']) && !$this->cObj->checkIf($conf['if.'])) {
37  return '';
38  }
39  // Store the original "currentFile" within a variable so it can be re-applied later-on
40  $originalFileInContentObject = $this->cObj->getCurrentFile();
41 
42  $fileCollector = $this->‪findAndSortFiles($conf);
43  $fileObjects = $fileCollector->getFiles();
44  $availableFileObjectCount = count($fileObjects);
45 
46  // optionSplit applied to conf to allow different settings per file
47  $splitConf = GeneralUtility::makeInstance(TypoScriptService::class)
48  ->explodeConfigurationForOptionSplit($conf, $availableFileObjectCount);
49 
50  $start = 0;
51  if (!empty($conf['begin'])) {
52  $start = (int)$conf['begin'];
53  }
54  if (!empty($conf['begin.'])) {
55  $start = (int)$this->cObj->stdWrap($start, $conf['begin.']);
56  }
57  $start = ‪MathUtility::forceIntegerInRange($start, 0, $availableFileObjectCount);
58 
59  $limit = $availableFileObjectCount;
60  if (!empty($conf['maxItems'])) {
61  $limit = (int)$conf['maxItems'];
62  }
63  if (!empty($conf['maxItems.'])) {
64  $limit = (int)$this->cObj->stdWrap($limit, $conf['maxItems.']);
65  }
66 
67  $end = ‪MathUtility::forceIntegerInRange($start + $limit, $start, $availableFileObjectCount);
68 
69  if (isset(‪$GLOBALS['TSFE'])) {
70  ‪$GLOBALS['TSFE']->register['FILES_COUNT'] = min($limit, $availableFileObjectCount);
71  }
72  $fileObjectCounter = 0;
73  $keys = array_keys($fileObjects);
74 
75  $content = '';
76  for ($i = $start; $i < $end; $i++) {
77  $key = $keys[$i];
78  $fileObject = $fileObjects[$key];
79 
80  if (isset(‪$GLOBALS['TSFE'])) {
81  ‪$GLOBALS['TSFE']->register['FILE_NUM_CURRENT'] = $fileObjectCounter;
82  }
83  $this->cObj->setCurrentFile($fileObject);
84  $content .= $this->cObj->cObjGetSingle($splitConf[$key]['renderObj'], $splitConf[$key]['renderObj.'], 'renderObj');
85  $fileObjectCounter++;
86  }
87 
88  // Reset current file within cObj to the original file after rendering output of FILES
89  // so e.g. stdWrap is not working on the last current file applied, thus avoiding side-effects
90  $this->cObj->setCurrentFile($originalFileInContentObject);
91 
92  return $this->cObj->stdWrap($content, $conf['stdWrap.'] ?? []);
93  }
94 
102  protected function ‪findAndSortFiles(array $conf)
103  {
104  $fileCollector = $this->‪getFileCollector();
105 
106  // Getting the files
107  if ((isset($conf['references']) && $conf['references']) || (isset($conf['references.']) && $conf['references.'])) {
108  /*
109  The TypoScript could look like this:
110  # all items related to the page.media field:
111  references {
112  table = pages
113  uid.data = page:uid
114  fieldName = media
115  }
116  # or: sys_file_references with uid 27:
117  references = 27
118  */
119  $referencesUidList = $this->cObj->stdWrapValue('references', $conf);
120  $referencesUids = ‪GeneralUtility::intExplode(',', $referencesUidList, true);
121  $fileCollector->addFileReferences($referencesUids);
122 
123  if (!empty($conf['references.'])) {
124  $this->‪addFileReferences($conf, (array)$this->cObj->data, $fileCollector);
125  }
126  }
127 
128  if ((isset($conf['files']) && $conf['files']) || (isset($conf['files.']) && $conf['files.'])) {
129  /*
130  The TypoScript could look like this:
131  # with sys_file UIDs:
132  files = 12,14,15# using stdWrap:
133  files.field = some_field
134  */
135  $fileUids = ‪GeneralUtility::intExplode(',', $this->cObj->stdWrapValue('files', $conf), true);
136  $fileCollector->addFiles($fileUids);
137  }
138 
139  if ((isset($conf['collections']) && $conf['collections']) || (isset($conf['collections.']) && $conf['collections.'])) {
140  $collectionUids = ‪GeneralUtility::intExplode(',', $this->cObj->stdWrapValue('collections', $conf), true);
141  $fileCollector->addFilesFromFileCollections($collectionUids);
142  }
143 
144  if ((isset($conf['folders']) && $conf['folders']) || (isset($conf['folders.']) && $conf['folders.'])) {
145  $folderIdentifiers = ‪GeneralUtility::trimExplode(',', $this->cObj->stdWrapValue('folders', $conf));
146  $fileCollector->addFilesFromFolders($folderIdentifiers, !empty($conf['folders.']['recursive']));
147  }
148 
149  // Enable sorting for multiple fileObjects
150  $sortingProperty = '';
151  if ((isset($conf['sorting']) && $conf['sorting']) || (isset($conf['sorting.']) && $conf['sorting.'])) {
152  $sortingProperty = $this->cObj->stdWrapValue('sorting', $conf);
153  }
154  if ($sortingProperty !== '') {
155  $sortingDirection = $conf['sorting.']['direction'] ?? '';
156  if (isset($conf['sorting.']['direction.'])) {
157  $sortingDirection = $this->cObj->stdWrap($sortingDirection, $conf['sorting.']['direction.']);
158  }
159  $fileCollector->sort($sortingProperty, $sortingDirection);
160  }
161 
162  return $fileCollector;
163  }
164 
172  protected function ‪addFileReferences(array $configuration, array $element, ‪FileCollector $fileCollector)
173  {
174 
175  // It's important that this always stays "fieldName" and not be renamed to "field" as it would otherwise collide with the stdWrap key of that name
176  $referencesFieldName = $this->cObj->stdWrapValue('fieldName', $configuration['references.']);
177 
178  // If no reference fieldName is set, there's nothing to do
179  if (empty($referencesFieldName)) {
180  return;
181  }
182 
183  $currentId = !empty($element['uid']) ? $element['uid'] : 0;
184  $tableName = $this->cObj->getCurrentTable();
185 
186  // Fetch the references of the default element
187  $referencesForeignTable = $this->cObj->stdWrapValue('table', $configuration['references.'], $tableName);
188  $referencesForeignUid = $this->cObj->stdWrapValue('uid', $configuration['references.'], $currentId);
189 
190  $pageRepository = $this->‪getPageRepository();
191  // Fetch element if definition has been modified via TypoScript
192  if ($referencesForeignTable !== $tableName || $referencesForeignUid !== $currentId) {
193  $element = $pageRepository->getRawRecord($referencesForeignTable, $referencesForeignUid);
194 
195  $pageRepository->versionOL($referencesForeignTable, $element, true);
196  if (is_array($element)) {
197  $element = $pageRepository->getLanguageOverlay($referencesForeignTable, $element);
198  }
199  }
200 
201  if (is_array($element)) {
202  $fileCollector->‪addFilesFromRelation($referencesForeignTable, $referencesFieldName, $element);
203  }
204  }
205 
209  protected function ‪getPageRepository()
210  {
211  return ‪$GLOBALS['TSFE']->sys_page;
212  }
213 
217  protected function ‪getFileCollector()
218  {
219  return GeneralUtility::makeInstance(FileCollector::class);
220  }
221 }
‪TYPO3\CMS\Frontend\ContentObject\FilesContentObject\addFileReferences
‪addFileReferences(array $configuration, array $element, FileCollector $fileCollector)
Definition: FilesContentObject.php:172
‪TYPO3\CMS\Frontend\ContentObject\FilesContentObject\getFileCollector
‪FileCollector getFileCollector()
Definition: FilesContentObject.php:217
‪TYPO3\CMS\Frontend\ContentObject
Definition: AbstractContentObject.php:16
‪TYPO3\CMS\Core\Utility\MathUtility\forceIntegerInRange
‪static int forceIntegerInRange($theInt, $min, $max=2000000000, $defaultValue=0)
Definition: MathUtility.php:32
‪TYPO3\CMS\Frontend\ContentObject\FilesContentObject\findAndSortFiles
‪FileCollector findAndSortFiles(array $conf)
Definition: FilesContentObject.php:102
‪TYPO3\CMS\Frontend\ContentObject\FilesContentObject\getPageRepository
‪TYPO3 CMS Core Domain Repository PageRepository getPageRepository()
Definition: FilesContentObject.php:209
‪TYPO3\CMS\Frontend\Resource\FileCollector
Definition: FileCollector.php:41
‪TYPO3\CMS\Frontend\Resource\FileCollector\addFilesFromRelation
‪addFilesFromRelation($relationTable, $relationField, array $referenceRecord)
Definition: FileCollector.php:97
‪TYPO3\CMS\Frontend\ContentObject\AbstractContentObject
Definition: AbstractContentObject.php:25
‪TYPO3\CMS\Core\Utility\GeneralUtility\trimExplode
‪static string[] trimExplode($delim, $string, $removeEmptyValues=false, $limit=0)
Definition: GeneralUtility.php:1059
‪TYPO3\CMS\Core\TypoScript\TypoScriptService
Definition: TypoScriptService.php:25
‪TYPO3\CMS\Frontend\ContentObject\FilesContentObject\render
‪string render($conf=[])
Definition: FilesContentObject.php:34
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Utility\GeneralUtility\intExplode
‪static int[] intExplode($delimiter, $string, $removeEmptyValues=false, $limit=0)
Definition: GeneralUtility.php:988
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:22
‪TYPO3\CMS\Frontend\ContentObject\FilesContentObject
Definition: FilesContentObject.php:27
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46