AfterCacheableContentIsGeneratedEvent 

The PSR-14 event \TYPO3\CMS\Frontend\Event\AfterCacheableContentIsGeneratedEvent can be used to decide if a page should be stored in cache.

It is executed right after all cacheable content is generated. It can also be used to manipulate the content before it is stored in TYPO3's page cache. In the Core, the event is used in EXT:indexed_search to index cacheable content.

The AfterCacheableContentIsGeneratedEvent contains the information if a generated page is able to store in cache via the $event->isCachingEnabled() method.

Example 

EXT:my_extension/Classes/Frontend/EventListener/MyEventListener.php
<?php

declare(strict_types=1);

namespace MyVendor\MyExtension\Frontend\EventListener;

use TYPO3\CMS\Core\Attribute\AsEventListener;
use TYPO3\CMS\Frontend\Event\AfterCacheableContentIsGeneratedEvent;

#[AsEventListener(
    identifier: 'my-extension/content-modifier',
)]
final readonly class MyEventListener
{
    public function __invoke(AfterCacheableContentIsGeneratedEvent $event): void
    {
        // Only do this when caching is enabled
        if (!$event->isCachingEnabled()) {
            return;
        }
        $event->setContent(str_replace(
            'foo',
            'bar',
            $event->getContent(),
        ));
    }
}
Copied!

New in version 13.0

The PHP attribute \TYPO3\CMS\Core\Attribute\AsEventListener has been introduced to tag a PHP class as an event listener. Alternatively, or if you need to be compatible with older TYPO3 versions, you can also register an event listener via the Configuration/Services.yaml file. Switch to an older version of this page for an example or have a look at the section Implementing an event listener in your extension.

API 

class AfterCacheableContentIsGeneratedEvent
Fully qualified name
\TYPO3\CMS\Frontend\Event\AfterCacheableContentIsGeneratedEvent

Event that allows to enhance or change content (also depending on enabled caching).

Depending on disable or enabling caching, the cache is then not stored in the pageCache.

Until TYPO3 v13, the flag "isCachingEnabled" was available in $TSFE->no_cache.

getRequest ( )
Returns
\Psr\Http\Message\ServerRequestInterface
getContent ( )
Returns
string
setContent ( string $content)
param $content

the content

isCachingEnabled ( )
Returns
bool
disableCaching ( )
enableCaching ( )
getCacheIdentifier ( )
Returns
string

Migration from AfterCacheableContentIsGeneratedEvent::getController() 

In most cases, method AfterCacheableContentIsGeneratedEvent->getController() was used in event listeners to get and/or set TypoScriptFrontendController->content at a late point within the frontend rendering chain.

The event has been changed by removing getController() and adding getContent() and setContent() instead.

 #[AsEventListener('my-extension')]
 public function indexPageContent(AfterCacheableContentIsGeneratedEvent $event): void
 {
-    $tsfe = $event->getController();
-    $content = $tsfe->content;
*    $content = $event->getContent();
     // ... $content is manipulated here
-    $tsfe->content = $content;
*    $event->setContent($content);
 }
Copied!

Extensions aiming for TYPO3 v13 and v14 compatibility in a single version can use a version check gate:

#[AsEventListener('my-extension')]
public function indexPageContent(AfterCacheableContentIsGeneratedEvent $event): void
{
    if ((new Typo3Version)->getMajorVersion() < 14) {
        // @todo: Remove if() when TYPO3 v13 compatibility is dropped, keep else body only
        $tsfe = $event->getController();
        $content = $tsfe->content;
    } else {
        $content = $event->getContent();
    }
    // ... $content is manipulated here
    if ((new Typo3Version)->getMajorVersion() < 14) {
        // @todo: Remove if() when TYPO3 v13 compatibility is dropped, keep else body only
        $tsfe = $event->getController();
        $tsfe->content = $content;
    } else {
        $event->setContent($content);
    }
}
Copied!