Skip to content

Instantly share code, notes, and snippets.

@t3easy
Created September 17, 2020 07:25
Show Gist options
  • Save t3easy/1618ced1d08c6609557b070a0763f318 to your computer and use it in GitHub Desktop.
Save t3easy/1618ced1d08c6609557b070a0763f318 to your computer and use it in GitHub Desktop.
Categories of files for TYPO3 v8
<?php
defined('TYPO3_MODE') or die();
if (TYPO3_MODE === 'FE' && !isset($_REQUEST['eID'])) {
\TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\SignalSlot\Dispatcher::class)->connect(
\TYPO3\CMS\Core\Resource\Index\MetaDataRepository::class,
'recordPostRetrieval',
\Vendor\Extension\Aspect\FileCategoryAspect::class,
'addCategoriesToMetadata'
);
}
<?php
namespace Vendor\Extension\Aspect;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Utility\GeneralUtility;
class FileCategoryAspect
{
/**
* Add categories to file metadata
*
* @param \ArrayObject $data
*/
public function addCategoriesToMetadata(\ArrayObject $data)
{
if ($data['categories'] > 0) {
$categories = $this->getCategories($data['uid']);
$this->languageAndWorkspaceOverlay($categories);
$data['categories'] = $categories;
} else {
$data['categories'] = [];
}
}
protected function getCategories(int $metaDataUid): ?array
{
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_category');
return $queryBuilder
->select('*')
->from('sys_category')
->join('sys_category',
'sys_category_record_mm',
'sys_category_record_mm',
$queryBuilder->expr()->eq(
'sys_category_record_mm.uid_local',
'sys_category.uid'
)
)
->where(
$queryBuilder->expr()->eq(
'sys_category_record_mm.tablenames',
$queryBuilder->createNamedParameter('sys_file_metadata', \PDO::PARAM_STR)
),
$queryBuilder->expr()->eq(
'sys_category_record_mm.fieldname',
$queryBuilder->createNamedParameter('categories', \PDO::PARAM_STR)
),
$queryBuilder->expr()->eq(
'sys_category_record_mm.uid_foreign',
$queryBuilder->createNamedParameter($metaDataUid, \PDO::PARAM_INT)
)
)
->execute()
->fetchAll();
}
/**
* 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;
}
}
}
/**
* @return \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
*/
protected function getTsfe()
{
return $GLOBALS['TSFE'];
}
}
<f:if condition="{downloads}">
<ul>
<f:groupedFor as="groupedDownloads" groupBy="properties.categories.0.title" each="{downloads}" groupKey="category">
<li>
<span>{f:if(condition:'{category}', then: '{category}', else: 'No category')}</span>
<ul>
<f:for each="{groupedDownloads}" as="download">
<li>
<a title="{f:if(condition: download.title, then: download.title, else: download.name)}" href="{download.publicUrl}" target="_blank">{f:if(condition: download.title, then: download.title, else: download.name)}, {download.size -> f:format.bytes()}</a>
</li>
</f:for>
</ul>
</li>
</f:groupedFor>
</ul>
</f:if>
tt_content.mycontentelement =< lib.contentElement
tt_content.mycontentelement {
templateName = MyContentElement
dataProcessing {
10 = TYPO3\CMS\Frontend\DataProcessing\FilesProcessor
10 {
references.fieldName = media
as = downloads
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment