Commit 1039ba36 authored by Andreas Fernandez's avatar Andreas Fernandez Committed by Oliver Bartsch
Browse files

[FEATURE] Provide default queue for notifications

To allow dispatching notifications to the user the easy way, a new global
flash message queue, identified by
`TYPO3\CMS\Core\Messaging\FlashMessageQueue::NOTIFICATION_QUEUE`, is
introduced that takes the flash message and renders it as a notification on
the top-right edge of the backend.

Backend modules based on `TYPO3\CMS\Backend\Template\ModuleTemplate`
automatically gain advantage of this feature.

Resolves: #97595
Releases: main
Change-Id: I0e04f0412117649391c0f50fa4249b7832dded69
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/74582

Tested-by: core-ci's avatarcore-ci <typo3@b13.com>
Tested-by: Simon Schaufelberger's avatarSimon Schaufelberger <simonschaufi+typo3@gmail.com>
Tested-by: Oliver Bartsch's avatarOliver Bartsch <bo@cedev.de>
Reviewed-by: Simon Schaufelberger's avatarSimon Schaufelberger <simonschaufi+typo3@gmail.com>
Reviewed-by: Oliver Bartsch's avatarOliver Bartsch <bo@cedev.de>
parent fd5712c9
......@@ -32,6 +32,7 @@ use TYPO3\CMS\Core\Messaging\AbstractMessage;
use TYPO3\CMS\Core\Messaging\FlashMessage;
use TYPO3\CMS\Core\Messaging\FlashMessageQueue;
use TYPO3\CMS\Core\Messaging\FlashMessageService;
use TYPO3\CMS\Core\Page\JavaScriptModuleInstruction;
use TYPO3\CMS\Core\Page\PageRenderer;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\View\ResponsableViewInterface;
......@@ -158,6 +159,7 @@ final class ModuleTemplate implements ViewInterface, ResponsableViewInterface
if (!empty($updateSignalDetails['script'])) {
$this->pageRenderer->addJsFooterInlineCode('updateSignals', implode("\n", $updateSignalDetails['script']));
}
$this->dispatchNotificationMessages();
}
/**
......@@ -321,6 +323,7 @@ final class ModuleTemplate implements ViewInterface, ResponsableViewInterface
if (!empty($updateSignalDetails['script'])) {
$this->pageRenderer->addJsFooterInlineCode('updateSignals', implode("\n", $updateSignalDetails['script']));
}
$this->dispatchNotificationMessages();
return $this->pageRenderer->render();
}
......@@ -456,6 +459,19 @@ final class ModuleTemplate implements ViewInterface, ResponsableViewInterface
return '<h1 ' . ($inlineEdit ? 'class="t3js-title-inlineedit"' : '') . '>' . htmlspecialchars($text) . '</h1>';
}
/**
* Dispatches all messages in a special FlashMessageQueue to the PageRenderer to be rendered as inline notifications
*/
protected function dispatchNotificationMessages(): void
{
$notificationQueue = $this->flashMessageService->getMessageQueueByIdentifier(FlashMessageQueue::NOTIFICATION_QUEUE);
foreach ($notificationQueue->getAllMessagesAndFlush() as $message) {
$notificationInstruction = JavaScriptModuleInstruction::create('@typo3/backend/notification.js');
$notificationInstruction->invoke('showMessage', $message->getTitle(), $message->getMessage(), $message->getSeverity());
$this->pageRenderer->getJavaScriptRenderer()->addJavaScriptModuleInstruction($notificationInstruction);
}
}
/**
* @deprecated since v12, will be removed in v13.
*/
......
......@@ -27,6 +27,9 @@ use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
*/
class FlashMessageQueue extends \SplQueue implements \JsonSerializable
{
public const FLASHMESSAGE_QUEUE = 'core.template.flashMessages';
public const NOTIFICATION_QUEUE = 'core.template.notifications';
/**
* A unique identifier for this queue
*
......
......@@ -39,7 +39,7 @@ class FlashMessageService implements SingletonInterface
* @param string $identifier
* @return FlashMessageQueue
*/
public function getMessageQueueByIdentifier($identifier = 'core.template.flashMessages'): FlashMessageQueue
public function getMessageQueueByIdentifier($identifier = FlashMessageQueue::FLASHMESSAGE_QUEUE): FlashMessageQueue
{
if (!isset($this->flashMessageQueues[$identifier])) {
$this->flashMessageQueues[$identifier] = GeneralUtility::makeInstance(
......
.. include:: /Includes.rst.txt
.. _feature-97595-1652121042
=========================================================
Feature: #97595 - Provide default queue for notifications
=========================================================
See :issue:`97595`
Description
===========
To allow dispatching notifications to the user the easy way, a new global flash
message queue, identified by
:php:`TYPO3\CMS\Core\Messaging\FlashMessageQueue::NOTIFICATION_QUEUE`, is
introduced that takes the flash message and renders it as a notification on the
top-right edge of the backend.
Backend modules based on :php:`TYPO3\CMS\Backend\Template\ModuleTemplate`
automatically gain advantage of this feature.
Example
=======
.. code-block:: php
$flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
$notificationQueue = $flashMessageService->getMessageQueueByIdentifier(FlashMessageQueue::NOTIFICATION_QUEUE);
$flashMessage = GeneralUtility::makeInstance(
FlashMessage::class,
'I\'m a message rendered as notification',
'Hooray!',
FlashMessage::OK
);
$notificationQueue->enqueue($flashMessage);
Impact
======
All flash messages dispatched to the flash message queue
:php:`FlashMessageQueue::NOTIFICATION_QUEUE` will be rendered as notifications
in the browser.
.. index:: Backend, ext:backend
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment