-
Notifications
You must be signed in to change notification settings - Fork 653
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[!!!][FEATURE] Add PSR-14 events for the BackendController
This introduces two new events to interact with the backend controller page generation process. They mirror and replace the renderPreProcess and renderPostProcess hooks, which are removed. The vestigial constructorPostProcess hook is also removed entirely. Resolves: #97451 Releases: main Change-Id: I58bccf341b478eedc87c9a9dd81fac788bba0a91 Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/74797 Tested-by: Stefan Bürk <stefan@buerk.tech> Tested-by: core-ci <typo3@b13.com> Tested-by: Benni Mack <benni@typo3.org> Reviewed-by: Stefan Bürk <stefan@buerk.tech> Reviewed-by: Benni Mack <benni@typo3.org>
- Loading branch information
Showing
7 changed files
with
242 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
typo3/sysext/backend/Classes/Controller/Event/AfterBackendPageRenderEvent.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the TYPO3 CMS project. | ||
* | ||
* 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! | ||
*/ | ||
|
||
namespace TYPO3\CMS\Backend\Controller\Event; | ||
|
||
use TYPO3\CMS\Core\View\ViewInterface; | ||
|
||
/** | ||
* This event triggers after a page has been rendered. | ||
* | ||
* Listeners may update the page content string with a modified | ||
* version if appropriate. | ||
*/ | ||
final class AfterBackendPageRenderEvent | ||
{ | ||
public function __construct(private string $content, private readonly ViewInterface $view) | ||
{ | ||
} | ||
|
||
public function getContent(): string | ||
{ | ||
return $this->content; | ||
} | ||
|
||
public function setContent(string $content): void | ||
{ | ||
$this->content = $content; | ||
} | ||
|
||
public function getView(): ViewInterface | ||
{ | ||
return $this->view; | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
typo3/sysext/backend/Tests/Functional/Controller/BackendControllerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the TYPO3 CMS project. | ||
* | ||
* 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! | ||
*/ | ||
|
||
namespace TYPO3\CMS\Backend\Tests\Functional\Controller; | ||
|
||
use Symfony\Component\DependencyInjection\Container; | ||
use TYPO3\CMS\Backend\Controller\BackendController; | ||
use TYPO3\CMS\Backend\Controller\Event\AfterBackendPageRenderEvent; | ||
use TYPO3\CMS\Backend\Routing\Route; | ||
use TYPO3\CMS\Core\Core\Bootstrap; | ||
use TYPO3\CMS\Core\Core\SystemEnvironmentBuilder; | ||
use TYPO3\CMS\Core\EventDispatcher\ListenerProvider; | ||
use TYPO3\CMS\Core\Http\ServerRequest; | ||
use TYPO3\CMS\Core\Utility\GeneralUtility; | ||
use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase; | ||
|
||
class BackendControllerTest extends FunctionalTestCase | ||
{ | ||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
$this->setUpBackendUserFromFixture(1); | ||
Bootstrap::initializeLanguageObject(); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function backendPageRenderEventIsTriggered(): void | ||
{ | ||
/** @var Container $container */ | ||
$container = $this->getContainer(); | ||
|
||
$state = [ | ||
'after-backend-page-render-listener' => null, | ||
]; | ||
|
||
// Dummy listeners that just record that the event existed. | ||
$container->set( | ||
'after-backend-page-render-listener', | ||
static function (AfterBackendPageRenderEvent $event) use (&$state) { | ||
$state['after-backend-page-render-listener'] = $event; | ||
} | ||
); | ||
|
||
$eventListener = GeneralUtility::makeInstance(ListenerProvider::class); | ||
$eventListener->addListener(AfterBackendPageRenderEvent::class, 'after-backend-page-render-listener'); | ||
|
||
$request = (new ServerRequest()) | ||
->withAttribute('applicationType', SystemEnvironmentBuilder::REQUESTTYPE_BE) | ||
->withAttribute('route', new Route('/main', ['packageName' => 'typo3/cms-backend', '_identifier' => 'main'])); | ||
|
||
$subject = $this->get(BackendController::class); | ||
$subject->mainAction($request); | ||
|
||
self::assertInstanceOf(AfterBackendPageRenderEvent::class, $state['after-backend-page-render-listener']); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...ocumentation/Changelog/12.0/Breaking-97451-RemoveBackendControllerPageHooks.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
.. include:: /Includes.rst.txt | ||
|
||
======================================================= | ||
Breaking: #97451 - Removed BackendController page hooks | ||
======================================================= | ||
|
||
See :issue:`97451` | ||
|
||
Description | ||
=========== | ||
|
||
The hooks :php:`$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['typo3/backend.php']['constructorPostProcess']`, | ||
:php:`$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['typo3/backend.php']['renderPreProcess']`, and | ||
:php:`$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['typo3/backend.php']['renderPostProcess']` have | ||
been removed in favor of a new PSR-14 Event :php:`\TYPO3\CMS\Backend\Controller\Event\AfterBackendPageRenderEvent`. | ||
|
||
Additionally, the :php:`BackendController->addCss()` method has been removed without replacement, | ||
as it is no longer used. | ||
|
||
Impact | ||
====== | ||
|
||
Any hook implementation registered is not executed anymore in | ||
TYPO3 v12.0+. The extension scanner will report possible usages. | ||
|
||
Affected Installations | ||
====================== | ||
|
||
All TYPO3 installations using this hook in custom extension code. | ||
|
||
Migration | ||
========= | ||
|
||
The hooks are removed without deprecation in order to allow extensions | ||
to work with TYPO3 v11 (using the hook) and v12+ (using the new Event) | ||
when implementing the Event as well without any further deprecations. | ||
Use the :doc:`PSR-14 Event <../12.0/Feature-97451-PSR-14EventsForBackendPageController>` | ||
as an improved replacement. | ||
|
||
.. index:: Backend, PHP-API, FullyScanned, ext:backend |
53 changes: 53 additions & 0 deletions
53
...mentation/Changelog/12.0/Feature-97451-PSR-14EventsForBackendPageController.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
.. include:: /Includes.rst.txt | ||
|
||
================================================================== | ||
Feature: #97451 - PSR-14 Events for modifying backend page content | ||
================================================================== | ||
|
||
See :issue:`97451` | ||
|
||
Description | ||
=========== | ||
A new PSR-14 Event :php:`\TYPO3\CMS\Backend\Controller\Event\AfterBackendPageRenderEvent` has | ||
been introduced which serves as a direct replacement for the now removed | ||
:php:`$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['typo3/backend.php']['constructorPostProcess']`, | ||
:php:`$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['typo3/backend.php']['renderPreProcess']`, and | ||
:php:`$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['typo3/backend.php']['renderPostProcess']` | ||
:doc:`hooks <../12.0/Breaking-97451-RemoveBackendControllerPageHooks>`. | ||
|
||
The new Event triggers after the page is rendered and includes | ||
the rendered page body. Listeners may overwrite the page string if desired. | ||
|
||
Example | ||
======= | ||
|
||
Registration of the Event in your extension's :file:`Services.yaml`: | ||
|
||
.. code-block:: yaml | ||
MyVendor\MyPackage\MyEventListener: | ||
tags: | ||
- name: event.listener | ||
identifier: 'my-package/backend/after-backend-controller-render' | ||
The corresponding event listener class: | ||
|
||
.. code-block:: php | ||
use TYPO3\CMS\Backend\Controller\Event\AfterBackendPageRenderEvent; | ||
final class MyEventListener | ||
{ | ||
public function __invoke(AfterBackendPageRenderEvent $event): void | ||
{ | ||
$content = $event->getContent() . ' I was here'; | ||
$event->setContent($content); | ||
} | ||
} | ||
Impact | ||
====== | ||
|
||
It's now possible to modify the backend page using the new PSR-14 :php:`AfterBackendPageRenderEvent`. | ||
|
||
.. index:: Backend, PHP-API, ext:backend |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters