My Blogs

Getting referenced files in TYPO3

In the frontend context

In a frontend context, the \TYPO3\CMS\Core\DataHandling\DataHandler class cannot be used and there is no specific API to create a File Reference. You are on your own.

The simplest solution is to simply create a database entry into table “sys_file_reference” by using directly the database connection class provided by TYPO3 CMS.

A cleaner solution using Extbase requires far more work. An example can be found here: https://github.com/helhum/upload_example

This snippet shows how to retrieve FAL items that have been attached to some other element, in this case the “media” field of the “pages” table:

$fileRepository = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\FileRepository::class);
$fileObjects = $fileRepository->findByRelation('pages', 'media', $uid);

where $uid is the id of some page. The return value is an array of \TYPO3\CMS\Core\Resource\FileReference objects.

Listing files in a folder

These would be the shortest steps to get the list of files in a given folder: get the Storage, get a Folder object for some path in that Storage (path relative to Storage root), finally retrieve the files.

$resourceFactory = \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance();
$defaultStorage = $resourceFactory->getDefaultStorage();
$folder = $defaultStorage->getFolder('/some/path/in/storage/');
$files = $defaultStorage->getFilesInFolder($folder);

no responses

Leave a Comment

Your email address will not be published. Required fields are marked *