‪TYPO3CMS  ‪main
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  $frontendController = $this->‪hasTypoScriptFrontendController()
41  : null;
42  // Store the original "currentFile" within a variable so it can be re-applied later-on
43  $originalFileInContentObject = $this->cObj->getCurrentFile();
44 
45  $fileCollector = $this->‪findAndSortFiles($conf);
46  $fileObjects = $fileCollector->getFiles();
47  $availableFileObjectCount = count($fileObjects);
48 
49  // optionSplit applied to conf to allow different settings per file
50  $splitConf = GeneralUtility::makeInstance(TypoScriptService::class)
51  ->explodeConfigurationForOptionSplit($conf, $availableFileObjectCount);
52 
53  $start = (int)$this->cObj->stdWrapValue('begin', $conf, 0);
54  $start = ‪MathUtility::forceIntegerInRange($start, 0, $availableFileObjectCount);
55 
56  $limit = (int)$this->cObj->stdWrapValue('maxItems', $conf, $availableFileObjectCount);
57  $end = ‪MathUtility::forceIntegerInRange($start + $limit, $start, $availableFileObjectCount);
58 
59  if ($frontendController !== null) {
60  $frontendController->register['FILES_COUNT'] = min($limit, $availableFileObjectCount);
61  }
62  $fileObjectCounter = 0;
63  $keys = array_keys($fileObjects);
64 
65  $content = '';
66  for ($i = $start; $i < $end; $i++) {
67  $key = $keys[$i];
68  $fileObject = $fileObjects[$key];
69 
70  if ($frontendController !== null) {
71  $frontendController->register['FILE_NUM_CURRENT'] = $fileObjectCounter;
72  }
73  $this->cObj->setCurrentFile($fileObject);
74  $content .= $this->cObj->cObjGetSingle($splitConf[$key]['renderObj'], $splitConf[$key]['renderObj.'], 'renderObj');
75  $fileObjectCounter++;
76  }
77 
78  // Reset current file within cObj to the original file after rendering output of FILES
79  // so e.g. stdWrap is not working on the last current file applied, thus avoiding side-effects
80  $this->cObj->setCurrentFile($originalFileInContentObject);
81 
82  return $this->cObj->stdWrap($content, $conf['stdWrap.'] ?? []);
83  }
84 
91  protected function ‪findAndSortFiles(array $conf)
92  {
93  $fileCollector = $this->‪getFileCollector();
94 
95  // Getting the files
96  if ((isset($conf['references']) && $conf['references']) || (isset($conf['references.']) && $conf['references.'])) {
97  /*
98  The TypoScript could look like this:
99  # all items related to the page.media field:
100  references {
101  table = pages
102  uid.data = page:uid
103  fieldName = media
104  }
105  # or: sys_file_references with uid 27:
106  references = 27
107  */
108  $referencesUidList = (string)$this->cObj->stdWrapValue('references', $conf);
109  $referencesUids = ‪GeneralUtility::intExplode(',', $referencesUidList, true);
110  $fileCollector->addFileReferences($referencesUids);
111 
112  if (!empty($conf['references.'])) {
113  $this->‪addFileReferences($conf, (array)$this->cObj->data, $fileCollector);
114  }
115  }
116 
117  if ((isset($conf['files']) && $conf['files']) || (isset($conf['files.']) && $conf['files.'])) {
118  /*
119  The TypoScript could look like this:
120  # with sys_file UIDs:
121  files = 12,14,15# using stdWrap:
122  files.field = some_field
123  */
124  $fileUids = ‪GeneralUtility::intExplode(',', (string)$this->cObj->stdWrapValue('files', $conf), true);
125  $fileCollector->addFiles($fileUids);
126  }
127 
128  if ((isset($conf['collections']) && $conf['collections']) || (isset($conf['collections.']) && $conf['collections.'])) {
129  $collectionUids = ‪GeneralUtility::intExplode(',', (string)$this->cObj->stdWrapValue('collections', $conf), true);
130  $fileCollector->addFilesFromFileCollections($collectionUids);
131  }
132 
133  if ((isset($conf['folders']) && $conf['folders']) || (isset($conf['folders.']) && $conf['folders.'])) {
134  $folderIdentifiers = ‪GeneralUtility::trimExplode(',', (string)$this->cObj->stdWrapValue('folders', $conf));
135  $fileCollector->addFilesFromFolders($folderIdentifiers, !empty($conf['folders.']['recursive']));
136  }
137 
138  // Enable sorting for multiple fileObjects
139  $sortingProperty = (string)$this->cObj->stdWrapValue('sorting', $conf);
140  if ($sortingProperty !== '') {
141  $sortingDirection = $this->cObj->stdWrapValue('direction', $conf['sorting.'] ?? []);
142  $fileCollector->sort($sortingProperty, $sortingDirection);
143  }
144 
145  return $fileCollector;
146  }
147 
154  protected function ‪addFileReferences(array $configuration, array $element, ‪FileCollector $fileCollector)
155  {
156  // 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
157  $referencesFieldName = $this->cObj->stdWrapValue('fieldName', $configuration['references.'] ?? []);
158 
159  // If no reference fieldName is set, there's nothing to do
160  if (empty($referencesFieldName)) {
161  return;
162  }
163 
164  $currentId = !empty($element['uid']) ? $element['uid'] : 0;
165  $tableName = $this->cObj->getCurrentTable();
166 
167  // Fetch the references of the default element
168  $referencesForeignTable = (string)$this->cObj->stdWrapValue('table', $configuration['references.'], $tableName);
169  $referencesForeignUid = (int)$this->cObj->stdWrapValue('uid', $configuration['references.'], $currentId);
170 
171  $pageRepository = $this->‪getPageRepository();
172  // Fetch element if definition has been modified via TypoScript
173  if (
174  ($referencesForeignTable !== '' && $referencesForeignTable !== $tableName)
175  || ($referencesForeignUid !== 0 && $referencesForeignUid !== $currentId)
176  ) {
177  $element = $pageRepository->getRawRecord($referencesForeignTable, $referencesForeignUid);
178  // Do versionOL() again and unset move pointers
179  $pageRepository->versionOL($referencesForeignTable, $element, true);
180  if (is_array($element)) {
181  $element = $pageRepository->getLanguageOverlay($referencesForeignTable, $element);
182  }
183  }
184 
185  if (is_array($element)) {
186  $fileCollector->‪addFilesFromRelation($referencesForeignTable ?: $tableName, $referencesFieldName, $element);
187  }
188  }
189 
190  protected function ‪getFileCollector(): ‪FileCollector
191  {
192  return GeneralUtility::makeInstance(FileCollector::class);
193  }
194 }
‪TYPO3\CMS\Frontend\ContentObject\FilesContentObject\addFileReferences
‪addFileReferences(array $configuration, array $element, FileCollector $fileCollector)
Definition: FilesContentObject.php:154
‪TYPO3\CMS\Frontend\ContentObject
Definition: AbstractContentObject.php:18
‪TYPO3\CMS\Frontend\ContentObject\FilesContentObject\findAndSortFiles
‪FileCollector findAndSortFiles(array $conf)
Definition: FilesContentObject.php:91
‪TYPO3\CMS\Frontend\ContentObject\AbstractContentObject\getPageRepository
‪getPageRepository()
Definition: AbstractContentObject.php:88
‪TYPO3\CMS\Frontend\ContentObject\AbstractContentObject\hasTypoScriptFrontendController
‪hasTypoScriptFrontendController()
Definition: AbstractContentObject.php:71
‪TYPO3\CMS\Frontend\Resource\FileCollector
Definition: FileCollector.php:47
‪TYPO3\CMS\Frontend\ContentObject\FilesContentObject\getFileCollector
‪getFileCollector()
Definition: FilesContentObject.php:190
‪TYPO3\CMS\Frontend\Resource\FileCollector\addFilesFromRelation
‪addFilesFromRelation(string $relationTable, string $relationField, array $referenceRecord)
Definition: FileCollector.php:88
‪TYPO3\CMS\Frontend\ContentObject\AbstractContentObject
Definition: AbstractContentObject.php:31
‪TYPO3\CMS\Core\TypoScript\TypoScriptService
Definition: TypoScriptService.php:27
‪TYPO3\CMS\Frontend\ContentObject\FilesContentObject\render
‪string render($conf=[])
Definition: FilesContentObject.php:34
‪TYPO3\CMS\Frontend\ContentObject\AbstractContentObject\getTypoScriptFrontendController
‪getTypoScriptFrontendController()
Definition: AbstractContentObject.php:79
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:24
‪TYPO3\CMS\Core\Utility\MathUtility\forceIntegerInRange
‪static int forceIntegerInRange(mixed $theInt, int $min, int $max=2000000000, int $defaultValue=0)
Definition: MathUtility.php:34
‪TYPO3\CMS\Frontend\ContentObject\FilesContentObject
Definition: FilesContentObject.php:27
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Utility\GeneralUtility\intExplode
‪static list< int > intExplode(string $delimiter, string $string, bool $removeEmptyValues=false)
Definition: GeneralUtility.php:751
‪TYPO3\CMS\Core\Utility\GeneralUtility\trimExplode
‪static list< string > trimExplode(string $delim, string $string, bool $removeEmptyValues=false, int $limit=0)
Definition: GeneralUtility.php:817