Peter Kraume
Peter Kraume

Reputation: 3747

TYPO3: clear cache of a page when file metadata is changed

I have a custom plugin which shows files for download based on sys_category.

When an editor changes the meta data of a file, e.g. changes the title or category, the changes are only reflected in the frontend when the complete frontend cache is cleared.

I've tried to add this to page TSconfig:

[page|uid = 0]
TCEMAIN.clearCacheCmd = 17
[global]

But this doesn't work. Any other idea how to clear the cache, when a sys_file_metadata record is changed?

Upvotes: 0

Views: 583

Answers (1)

Peter Kraume
Peter Kraume

Reputation: 3747

Here is my solution. Thx Aristeidis for the hint.

ext_localconf.php

$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['processDatamapClass']['my_extension_key'] =
    \Vendor\ExtKey\Hooks\DataHandler::class;

Classes/Hooks/DataHandler.php

<?php

namespace Vendor\ExtKey\Hooks;

use TYPO3\CMS\Core\Cache\CacheManager;
use TYPO3\CMS\Core\Utility\GeneralUtility;

class DataHandler
{
    public function processDatamap_afterDatabaseOperations(
        $status,
        $table,
        $recordUid,
        array $fields,
        \TYPO3\CMS\Core\DataHandling\DataHandler $parentObject
    ) {
        if ($table === 'sys_file_metadata') {
            // hardcoded list of page uids to clear
            $pageIdsToClear = [17];

            if (!is_array($pageIdsToClear)) {
                $pageIdsToClear = [(int)$pageIdsToClear];
            }
            $tags = array_map(function ($item) {
                return 'pageId_' . $item;
            }, $pageIdsToClear);
            GeneralUtility::makeInstance(CacheManager::class)->flushCachesInGroupByTags('pages', $tags);
        }
    }
}

Of course this could be improved more:

  • Currently the list of page uids is hardcoded. That could be made configureable via extension manager settings.
  • A check could be implemented to only delete the cache if the file has a certain sys_category assigned, e.g. Downloads

But for the moment this solution is enough for my needs.

Upvotes: 1

Related Questions