description |
---|
You can send notifications to users who work with the back office by using notification bars or notifications in the user menu. |
You can send two types on notifications to the users.
Notification bar is displayed in specific situations as a message bar appearing at the bottom of the page. It appears to whoever is doing a specific operation in the back office.
Custom notifications are sent to a specific user. They appear in their profile in the back office.
Notifications are displayed as a message bar in the back office.
There are four types of notifications: info
, success
, warning
and error
.
To send a notification from PHP, inject the TranslatableNotificationHandlerInterface
into your class.
$this->notificationHandler->info(
/** @Desc("Notification text") */
'example.notification.text',
[],
'domain'
);
To have the notification translated, provide the message strings in the translation files under the correct domain and key.
To create a notification from the front end (in this example, of type info
), use the following code:
const eventInfo = new CustomEvent('ibexa-notify', {
detail: {
label: 'info',
message: 'Notification text'
}
});
Dispatch the event with document.body.dispatchEvent(eventInfo);
.
You can send your own custom notifications to the user which are displayed in the user menu.
To create a new notification you must use the createNotification(Ibexa\Contracts\Core\Repository\Values\Notification\CreateStruct $createStruct)
method from Ibexa\Contracts\Core\Repository\NotificationService
.
Example:
[[= include_file('code_samples/back_office/notifications/src/EventListener/ContentPublishEventListener.php') =]]
To display the notification, write a renderer and tag it as a service.
The example below presents a renderer that uses Twig to render a view:
[[= include_file('code_samples/back_office/notifications/src/Notification/MyRenderer.php') =]]
You can add the template that is defined above in the render()
method to one of your custom bundles:
[[= include_file('code_samples/back_office/notifications/templates/themes/admin/notification.html.twig') =]]
Finally, you need to add an entry to config/services.yaml
:
[[= include_file('code_samples/back_office/notifications/config/custom_services.yaml') =]]
To define the timeout for hiding Back-Office notification bars, per notification type, use the ibexa.system.<scope>.notifications.<notification_type>.timeout
configuration key:
ibexa:
system:
admin:
notifications:
error:
timeout: 0
warning:
timeout: 0
success:
timeout: 5000
info:
timeout: 0
The values shown above are the defaults.
0
means the notification doesn't hide automatically.