Created
August 7, 2020 10:18
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
tt_content { | |
own_element =< lib.contentElement | |
own_element { | |
templateName = OwnElement | |
dataProcessing { | |
10 = TYPO3\CMS\Frontend\DataProcessing\FilesProcessor | |
10 { | |
references.fieldName = assets | |
as = file | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- 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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
services: | |
Vendor\Package\EventListener\AddCategoriesToFile: | |
tags: | |
- name: event.listener | |
identifier: 'AddCategoriesToFile' | |
event: TYPO3\CMS\Core\Resource\Event\EnrichFileMetaDataEvent | |
after: 'languageAndWorkspaceOverlay' |
@t3easy Can be possible. Maybe adding a where condition for the current language.
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); )
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
Is it possible, that https://gist.github.com/spoonerWeb/41f37acf918efb8e6cc625ac37300d10#file-addcategoriestofile-php-L31 don't handle translation?