Skip to content

Instantly share code, notes, and snippets.

@spoonerWeb
Created August 7, 2020 10:18
Show Gist options
  • Select an option

  • Save spoonerWeb/41f37acf918efb8e6cc625ac37300d10 to your computer and use it in GitHub Desktop.

Select an option

Save spoonerWeb/41f37acf918efb8e6cc625ac37300d10 to your computer and use it in GitHub Desktop.

Revisions

  1. spoonerWeb created this gist Aug 7, 2020.
    45 changes: 45 additions & 0 deletions AddCategoriesToFile.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    <?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();
    }
    }
    13 changes: 13 additions & 0 deletions ContentElementWithFile.typoscript
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    tt_content {
    own_element =< lib.contentElement
    own_element {
    templateName = OwnElement
    dataProcessing {
    10 = TYPO3\CMS\Frontend\DataProcessing\FilesProcessor
    10 {
    references.fieldName = assets
    as = file
    }
    }
    }
    }
    6 changes: 6 additions & 0 deletions OwnElement.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    <!-- 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>
    7 changes: 7 additions & 0 deletions Services.yaml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    services:
    Vendor\Package\EventListener\AddCategoriesToFile:
    tags:
    - name: event.listener
    identifier: 'AddCategoriesToFile'
    event: TYPO3\CMS\Core\Resource\Event\EnrichFileMetaDataEvent
    after: 'languageAndWorkspaceOverlay'