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