Skip to content

Instantly share code, notes, and snippets.

@mneuhaus
Created September 26, 2013 13:42
Show Gist options
  • Save mneuhaus/6714375 to your computer and use it in GitHub Desktop.
Save mneuhaus/6714375 to your computer and use it in GitHub Desktop.
This is a little "hack" i used recently to render the default Uploads Content element using Fluid instead of TypoScript
<?php
require_once(\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath('famelo') . '/Classes/Content/Uploads.php');
?>
tt_content {
uploads {
20 = USER
20 {
userFunc = Famelo\Content\Uploads->render
file = famelo/Resources/Private/Content/Uploads.html
}
}
}
<ul class="list-group">
<f:for each="{files}" as="file">
<li class="list-group-item">
<a href="{file.publicUrl}" >
<img src="typo3/gfx/fileicons/{file.extension}.gif" />
{file.name}
</a>
</li>
</f:for>
</ul>
<?php
namespace Famelo\Sts\Content;
/* *
* This script is backported from the FLOW3 package "TYPO3.Fluid". *
* *
* It is free software; you can redistribute it and/or modify it under *
* the terms of the GNU Lesser General Public License, either version 3 *
* of the License, or (at your option) any later version. *
* *
* The TYPO3 project - inspiring people to share! *
* */
/*
*/
class Uploads extends \TYPO3\CMS\Frontend\ContentObject\FilesContentObject {
public $cObj;
public function __construct() {
$this->fileFactory = \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance();
}
/**
* @param string $content
* @param array $conf
* @return string
*/
public function render($content = '', $conf = array()) {
$this->view = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Fluid\\View\\StandaloneView');
$file = isset($conf['file.']) ? $this->cObj->stdWrap($conf['file'], $conf['file.']) : $conf['file'];
$templateService = $GLOBALS['TSFE']->tmpl;
$this->view->setTemplatePathAndFilename($templateService->getFileName($file));
$this->view->assign('files', $this->getFiles($conf));
return $this->view->render();
}
/**
* Rendering the cObject FILES
*
* @param array $conf Array of TypoScript properties
* @return string Output
*/
public function getFiles($conf = array()) {
/** @var \TYPO3\CMS\Core\Resource\FileRepository $fileRepository */
$fileRepository = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\FileRepository');
$fileObjects = array();
// Getting the files
if ($conf['references'] || $conf['references.']) {
/*
The TypoScript could look like this:# all items related to the page.media field:
references {
table = pages
uid.data = page:uid
fieldName = media
}# or: sys_file_references with uid 27:
references = 27
*/
$referencesUid = $this->stdWrapValue('references', $conf);
$referencesUidArray = \TYPO3\CMS\Core\Utility\GeneralUtility::intExplode(',', $referencesUid, TRUE);
foreach ($referencesUidArray as $referenceUid) {
try {
$this->addToArray($fileRepository->findFileReferenceByUid($referenceUid), $fileObjects);
} catch (\TYPO3\CMS\Core\Resource\Exception $e) {
/** @var \TYPO3\CMS\Core\Log\Logger $logger */
$logger = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\CMS\Core\Log\LogManager')->getLogger();
$logger->warning('The file-reference with uid "' . $referenceUid . '" could not be found and won\'t be included in frontend output');
}
}
// 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
$referencesFieldName = $this->stdWrapValue('fieldName', $conf['references.']);
if ($referencesFieldName) {
$table = $this->cObj->getCurrentTable();
if ($table === 'pages' && isset($this->cObj->data['_LOCALIZED_UID']) && intval($this->cObj->data['sys_language_uid']) > 0) {
$table = 'pages_language_overlay';
}
$referencesForeignTable = $this->stdWrapValue('table', $conf['references.'], $table);
$referencesForeignUid = $this->stdWrapValue('uid', $conf['references.'], isset($this->cObj->data['_LOCALIZED_UID']) ? $this->cObj->data['_LOCALIZED_UID'] : $this->cObj->data['uid']);
$this->addToArray($fileRepository->findByRelation($referencesForeignTable, $referencesFieldName, $referencesForeignUid), $fileObjects);
}
}
if ($conf['files'] || $conf['files.']) {
/*
The TypoScript could look like this:
# with sys_file UIDs:
files = 12,14,15# using stdWrap:
files.field = some_field
*/
$fileUids = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $this->stdWrapValue('files', $conf), TRUE);
foreach ($fileUids as $fileUid) {
try {
$this->addToArray($fileRepository->findByUid($fileUid), $fileObjects);
} catch (\TYPO3\CMS\Core\Resource\Exception $e) {
/** @var \TYPO3\CMS\Core\Log\Logger $logger */
$logger = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\CMS\Core\Log\LogManager')->getLogger();
$logger->warning('The file with uid "' . $fileUid . '" could not be found and won\'t be included in frontend output');
}
}
}
if ($conf['collections'] || $conf['collections.']) {
$collectionUids = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $this->stdWrapValue('collections', $conf), TRUE);
/** @var \TYPO3\CMS\Core\Resource\FileCollectionRepository $collectionRepository */
$collectionRepository = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\FileCollectionRepository');
foreach ($collectionUids as $collectionUid) {
try {
$fileCollection = $collectionRepository->findByUid($collectionUid);
if ($fileCollection instanceof \TYPO3\CMS\Core\Resource\Collection\AbstractFileCollection) {
$fileCollection->loadContents();
$this->addToArray($fileCollection->getItems(), $fileObjects);
}
} catch (\TYPO3\CMS\Core\Resource\Exception $e) {
/** @var \TYPO3\CMS\Core\Log\Logger $logger */
$logger = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\CMS\Core\Log\LogManager')->getLogger();
$logger->warning('The file-collection with uid "' . $collectionUid . '" could not be found or contents could not be loaded and won\'t be included in frontend output');
}
}
}
if ($conf['folders'] || $conf['folders.']) {
$folderIdentifiers = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $this->stdWrapValue('folders', $conf));
/** @var \TYPO3\CMS\Core\Resource\ResourceFactory $fileFactory */
$fileFactory = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\ResourceFactory');
foreach ($folderIdentifiers as $folderIdentifier) {
if ($folderIdentifier) {
try {
$folder = $fileFactory->getFolderObjectFromCombinedIdentifier($folderIdentifier);
if ($folder instanceof \typo3\CMS\Core\Resource\Folder) {
$this->addToArray($folder->getFiles(), $fileObjects);
}
} catch (\TYPO3\CMS\Core\Resource\Exception $e) {
/** @var \TYPO3\CMS\Core\Log\Logger $logger */
$logger = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\CMS\Core\Log\LogManager')->getLogger();
$logger->warning('The folder with identifier "' . $folderIdentifier . '" could not be found and won\'t be included in frontend output');
}
}
}
}
// Rendering the files
$content = '';
// optionSplit applied to conf to allow differnt settings per file
$splitConf = $GLOBALS['TSFE']->tmpl->splitConfArray($conf, count($fileObjects));
// Enable sorting for multiple fileObjects
$sortingProperty = '';
if ($conf['sorting'] || $conf['sorting.']) {
$sortingProperty = $this->stdWrapValue('sorting', $conf);
}
if ($sortingProperty !== '' && count($fileObjects) > 1) {
usort($fileObjects, function(\TYPO3\CMS\Core\Resource\FileInterface $a, \TYPO3\CMS\Core\Resource\FileInterface $b) use($sortingProperty) {
if ($a->hasProperty($sortingProperty) && $b->hasProperty($sortingProperty)) {
return strnatcasecmp($a->getProperty($sortingProperty), $b->getProperty($sortingProperty));
} else {
return 0;
}
});
}
return $fileObjects;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment