AfterCacheableContentIsGeneratedEvent
New in version 12.0
This event together with AfterCachedPageIsPersistedEvent has been introduced to serve as a direct replacement for the removed hooks:
The PSR-14 event
\TYPO3\
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 After
contains the
information if a generated page is able to store in cache via the
$event->is
method. This can be used to
differentiate between the previous hooks content
and
content
. The later hook was called regardless of whether the
cache was enabled or not.
Example
Note
Currently, the example below is outdated. It uses a - now internal -
property Typo
.
<?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->getController()->content = str_replace(
'foo',
'bar',
$event->getController()->content,
);
}
}
New in version 13.0
The PHP attribute \TYPO3\
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/
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\ After Cacheable Content Is Generated Event
Event that allows to enhance or change content (also depending on enabled caching).
Think of $this->isCachingEnabled() as the same as $TSFE->no_cache. Depending on disable or enabling caching, the cache is then not stored in the pageCache.