Skip to content

Commit

Permalink
[FEATURE] Allow modifying fetched page content (#5110)
Browse files Browse the repository at this point in the history
* [TASK] Update code snippets

References: TYPO3-Documentation/Changelog-To-Doc#1165
Releases: main, 13.4

* Apply suggestions from code review

Co-authored-by: Stefan Frömken <froemken@gmail.com>

---------

Co-authored-by: Stefan Frömken <froemken@gmail.com>
  • Loading branch information
linawolf and froemken authored Dec 10, 2024
1 parent 47d6d00 commit e9efa02
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
.. include:: /Includes.rst.txt
.. index:: Events; AfterContentHasBeenFetchedEvent
.. _AfterContentHasBeenFetchedEvent:

===============================
AfterContentHasBeenFetchedEvent
===============================

.. versionadded:: 13.4.2 / 14.0

Using the PSR-14 :php:`\TYPO3\CMS\Frontend\Event\AfterContentHasBeenFetchedEvent`,
it is possible to manipulate the page content, which has been fetched by the
`page-content data processor <https://docs.typo3.org/permalink/t3tsref:PageContentFetchingProcessor>`_,
based on the page layout and corresponding columns configuration.

.. _AfterContentHasBeenFetchedEvent-example:

Example
=======

The event listener class, using the PHP attribute :php:`#[AsEventListener]` for
registration, removes some of the fetched page content elements based on
specific field values.

.. literalinclude:: _AfterContentHasBeenFetchedEvent/_MyEventListener.php
:language: php
:caption: EXT:my_extension/Classes/Frontend/EventListener/MyEventListener.php

.. _AfterContentHasBeenFetchedEvent-api:

API
===

.. include:: /CodeSnippets/Events/Frontend/AfterContentHasBeenFetchedEvent.rst.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace MyVendor\MyExtension\Frontend\EventListener;

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

final readonly class MyEventListener
{
#[AsEventListener]
public function removeFetchedPageContent(AfterContentHasBeenFetchedEvent $event): void
{
foreach ($event->groupedContent as $columnIdentifier => $column) {
foreach ($column['records'] ?? [] as $key => $record) {
if ($record->has('parent_field_name') && (int)($record->get('parent_field_name') ?? 0) > 0) {
unset($event->groupedContent[$columnIdentifier]['records'][$key]);
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,10 @@
'targetFileName' => 'CodeSnippets/Events/Frontend/AfterTypoScriptDeterminedEvent.rst.txt',
'withCode' => false,
],
[
'action' => 'createPhpClassDocs',
'class' => \TYPO3\CMS\Frontend\Event\AfterContentHasBeenFetchedEvent::class,
'targetFileName' => 'CodeSnippets/Events/Frontend/AfterContentHasBeenFetchedEvent.rst.txt',
'withCode' => false,
],
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.. Generated by https://github.com/TYPO3-Documentation/t3docs-codesnippets
.. php:namespace:: TYPO3\CMS\Frontend\Event
.. php:class:: AfterContentHasBeenFetchedEvent
Event listeners are able to manipulate fetched page content, which is already grouped by column

.. php:attr:: groupedContent
:public:

.. php:attr:: request
:readonly:
:public:

0 comments on commit e9efa02

Please sign in to comment.