src/EventSubscriber/EasyAdminSubscriberBlogPosted.php line 25

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\BlogArticle;
  4. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. class EasyAdminSubscriberBlogPosted implements EventSubscriberInterface
  7. {
  8.     /**
  9.      * @return string[][]
  10.      */
  11.     public static function getSubscribedEvents(): array
  12.     {
  13.         return [
  14.             BeforeEntityPersistedEvent::class => ['setBlogPostedAt'],
  15.         ];
  16.     }
  17.     /**
  18.      * @param BeforeEntityPersistedEvent $event
  19.      * @return void
  20.      */
  21.     public function setBlogPostedAt(BeforeEntityPersistedEvent $event): void
  22.     {
  23.         $blog $event->getEntityInstance();
  24.         if (!($blog instanceof BlogArticle)) {
  25.             return;
  26.         }
  27.         $blog->setCreatedAt(new \DateTime());
  28.     }
  29. }