Skip to content

Commit ad4ce60

Browse files
committed
Add docs for FlashMessageImportanceMapper
1 parent e7acfd9 commit ad4ce60

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

notifier.rst

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,61 @@ and
662662
:class:`Symfony\\Component\\Notifier\\Notification\\EmailNotificationInterface`
663663
also exists to modify messages sent to those channels.
664664

665+
Customize Browser Notifications (Flash Messages)
666+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
667+
668+
.. versionadded:: 6.1
669+
Support for customizing importance levels was introduced in Symfony 6.1.
670+
671+
The default behavior for Browser channel notifications is to add a flash
672+
message with a key of "notification".
673+
674+
However, it may be desirable to map the importance level of the notification
675+
to the type of flash message, so that you can style the different flash
676+
messages according to their importance.
677+
678+
This can be done by overriding the default
679+
``notifier.flash_message_importance_mapper`` service with your own implementation of
680+
:class:`Symfony\\Component\\Notifier\\FlashMessage\\FlashMessageImportanceMapperInterface`
681+
where you can provide your own "importance" to "alert level" mapping.
682+
683+
Symfony currently provides an implementation for the Bootstrap CSS framework's
684+
typical alert levels, which you can implement immediately using:
685+
686+
.. configuration-block::
687+
688+
.. code-block:: yaml
689+
690+
# config/services.yaml
691+
services:
692+
notifier.flash_message_importance_mapper: Symfony\Component\Notifier\FlashMessage\BootstrapFlashMessageImportanceMapper
693+
694+
.. code-block:: xml
695+
696+
<?xml version="1.0" encoding="UTF-8" ?>
697+
<container xmlns="http://symfony.com/schema/dic/services"
698+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
699+
xsi:schemaLocation="http://symfony.com/schema/dic/services
700+
https://symfony.com/schema/dic/services/services-1.0.xsd">
701+
702+
<services>
703+
<service id="notifier.flash_message_importance_mapper" class="Symfony\Component\Notifier\FlashMessage\BootstrapFlashMessageImportanceMapper"/>
704+
</services>
705+
</container>
706+
707+
.. code-block:: php
708+
709+
// config/services.php
710+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
711+
712+
use Symfony\Component\Notifier\FlashMessage\BootstrapFlashMessageImportanceMapper;
713+
714+
return function(ContainerConfigurator $configurator) {
715+
$configurator->services()
716+
->set('notifier.flash_message_importance_mapper', BootstrapFlashMessageImportanceMapper::class)
717+
;
718+
};
719+
665720
Disabling Delivery
666721
------------------
667722

0 commit comments

Comments
 (0)