Skip to content

Instantly share code, notes, and snippets.

Created August 7, 2020 10:18
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save spoonerWeb/41f37acf918efb8e6cc625ac37300d10 to your computer and use it in GitHub Desktop.
Add category records to TYPO3 file record (e.g. for using it when fetching files via DataProcessing)
namespace Vendor\Package\EventListener;
* This file is part of a TYPO3 extension.
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
* The TYPO3 project - inspiring people to share!
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Utility\GeneralUtility;
class AddCategoriesToFile
public function __invoke(\TYPO3\CMS\Core\Resource\Event\EnrichFileMetaDataEvent $event): void
$record = $event->getRecord();
if ($record['categories'] > 0) {
$record['categoryRecords'] = $this->getCategories($event->getMetaDataUid());
protected function getCategories(int $metaDataUid): ?array
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_file_metadata');
return $queryBuilder
->from('sys_category', 'cat')
->leftJoin('cat', 'sys_category_record_mm', 'catmm', 'catmm.uid_local = cat.uid')
$queryBuilder->expr()->eq('catmm.uid_foreign', $metaDataUid)
tt_content {
own_element =< lib.contentElement
own_element {
templateName = OwnElement
dataProcessing {
10 = TYPO3\CMS\Frontend\DataProcessing\FilesProcessor
10 {
references.fieldName = assets
as = file
<!-- Output a list of all categories -->
<f:if condition="{}">
<f:for each="{}" as="category" iteration="it">
<f:if condition="{it.isFirst}"><f:else>, </f:else></f:if>{category.title}
- name: event.listener
identifier: 'AddCategoriesToFile'
event: TYPO3\CMS\Core\Resource\Event\EnrichFileMetaDataEvent
after: 'languageAndWorkspaceOverlay'
Copy link

t3easy commented Sep 16, 2020

Copy link

@t3easy Can be possible. Maybe adding a where condition for the current language.

Copy link

t3easy commented Sep 16, 2020

I borrowed this from

     * Do translation and workspace overlay
     * @param array $categories
    public function languageAndWorkspaceOverlay(array &$categories)
        foreach ($categories as $key => $category) {
            $this->getTsfe()->sys_page->versionOL('sys_category', $category);
            $overlaidCategory = $this->getTsfe()->sys_page->getRecordOverlay(
            if ($overlaidCategory !== null){
                $categories[$key] = $overlaidCategory;

(but from TYPO3 8.7 so $this->getTsfe()-sys_page would be $pageRepository = GeneralUtility::makeInstance(PageRepository::class); )

Copy link

t3easy commented Sep 17, 2020

Here a full example for TYPO3 8 with language and workspace overlay:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment