Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Add category records to TYPO3 file record (e.g. for using it when fetching files via DataProcessing)
<?php
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());
}
$event->setRecord($record);
}
protected function getCategories(int $metaDataUid): ?array
{
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_file_metadata');
return $queryBuilder
->select('*')
->from('sys_category', 'cat')
->leftJoin('cat', 'sys_category_record_mm', 'catmm', 'catmm.uid_local = cat.uid')
->where(
$queryBuilder->expr()->eq('catmm.uid_foreign', $metaDataUid)
)
->execute()
->fetchAll();
}
}
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="{file.0.properties.categoryRecords}">
<f:for each="{file.0.properties.categoryRecords}" as="category" iteration="it">
<f:if condition="{it.isFirst}"><f:else>, </f:else></f:if>{category.title}
</f:for>
</f:if>
services:
Vendor\Package\EventListener\AddCategoriesToFile:
tags:
- name: event.listener
identifier: 'AddCategoriesToFile'
event: TYPO3\CMS\Core\Resource\Event\EnrichFileMetaDataEvent
after: 'languageAndWorkspaceOverlay'
@t3easy
Copy link

t3easy commented Sep 16, 2020

@spoonerWeb
Copy link
Author

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

@t3easy
Copy link

t3easy commented Sep 16, 2020

I borrowed this from https://github.com/TYPO3/TYPO3.CMS/blob/10.4/typo3/sysext/frontend/Classes/Aspect/FileMetadataOverlayAspect.php#L38

/**
     * 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(
                'sys_category',
                $category,
                $this->getTsfe()->sys_language_content,
                $this->getTsfe()->sys_language_contentOL
            );
            if ($overlaidCategory !== null){
                $categories[$key] = $overlaidCategory;
            }
        }
    }

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

@t3easy
Copy link

t3easy commented Sep 17, 2020

Here a full example for TYPO3 8 with language and workspace overlay:
https://gist.github.com/t3easy/1618ced1d08c6609557b070a0763f318

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