Jonathan Scheiber

EasyAdmin bundle: flash message after save

Read time: 3 minutes 2 comments
EasyAdmin bundle: flash message after save

By default, when creating content, EasyAdmin displays no message after creating, updating or removing entities. Let's see how to manage that :)

You can see an example in the article overview image.

The goal of this article will be to automate these flash messages with as little code as possible.

Some explanations before starting

The code has been tested with Symfony 5.2 and EasyAdmin bundle 3.2.8. It is based on the following facts:

Code

<?php

declare(strict_types=1);

namespace App\Listener;

use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityDeletedEvent;
use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityPersistedEvent;
use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityUpdatedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Translation\TranslatableMessage;

final class EasyAdminListener implements EventSubscriberInterface
{
    public function __construct(private SessionInterface $session)
    {
    }

    public static function getSubscribedEvents(): array
    {
        return [
            AfterEntityPersistedEvent::class => ['flashMessageAfterPersist'],
            AfterEntityUpdatedEvent::class => ['flashMessageAfterUpdate'],
            AfterEntityDeletedEvent::class => ['flashMessageAfterDelete'],
        ];
    }

    public function flashMessageAfterPersist(AfterEntityPersistedEvent $event): void
    {
        $this->session->getFlashBag()->add('success', new TranslatableMessage('content_admin.flash_message.create', [
            '%name%' => (string) $event->getEntityInstance(),
        ], 'admin'));
    }

    public function flashMessageAfterUpdate(AfterEntityUpdatedEvent $event): void
    {
        $this->session->getFlashBag()->add('success', new TranslatableMessage('content_admin.flash_message.update', [
            '%name%' => (string) $event->getEntityInstance(),
        ], 'admin'));
    }

    public function flashMessageAfterDelete(AfterEntityDeletedEvent $event): void
    {
        $this->session->getFlashBag()->add('success', new TranslatableMessage('content_admin.flash_message.delete', [
            '%name%' => (string) $event->getEntityInstance(),
        ], 'admin'));
    }
}
# Translations, put in translations/admin.en.yaml
content_admin:
    flash_message:
        create: Content "%name%" has been created.
        update: Content "%name%" has been updated.
        delete: Content "%name%" has been removed.

How does it work?

The listener will listen to the three EasyAdmin events warning that a Doctrine entity has been created, updated or deleted. Each of these events has access to the impacted entity via the getEntityInstance() method available on the event.

From there, a flash message is saved in session, passing as a parameter of the flash message the value returned by the __toString() method of the entity.

As soon as autoconfigure is activated in your project, it's over! You have your beautiful flash messages that appear when you save an entity.

Et voilà! :) If you have any questions, comments, suggestions... Feel free to post a comment below :)

Comments

Posted by Robin Bastiaan on 25/04/2021 at 13:32.
Elegant solution I will use for sure. Thanks for sharing!
Posted by wietsedc on 26/04/2021 at 09:57.
thanks! details like this make the user experience so much better, I implemented it straight away.

Add a comment