src/EventSubscriber/EasyAdminSubscriber.php line 32

Open in your IDE?
  1. <?php
  2. # src/EventSubscriber/EasyAdminSubscriber.php
  3. namespace App\EventSubscriber;
  4. use App\Controller\Admin\ProductCrudController;
  5. use App\Entity\Product;
  6. use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
  7. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeCrudActionEvent;
  8. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent;
  9. use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\HttpFoundation\RedirectResponse;
  12. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  13. class EasyAdminSubscriber implements EventSubscriberInterface
  14. {
  15.     private $adminUrlGenerator;
  16.     public function __construct(AdminUrlGenerator $adminUrlGenerator)
  17.     {
  18.         $this->adminUrlGenerator $adminUrlGenerator;
  19.     }
  20.     public static function getSubscribedEvents()
  21.     {
  22.         return [
  23.             BeforeCrudActionEvent::class => ['redirectDetail'],
  24.         ];
  25.     }
  26.     public function redirectDetail(BeforeCrudActionEvent $event)
  27.     {
  28.         $entity $event->getAdminContext()->getEntity()->getInstance();
  29.         if ($event->getAdminContext()->getCrud()->getCurrentAction() == "detail") {
  30.             $entityName $event->getAdminContext()->getEntity()->getFqcn();
  31.             $entityName explode("\\"$entityName);
  32.             $entityName $entityName[2];
  33.             $url $this->adminUrlGenerator
  34.                 ->setController(("App\Controller\Admin\\" $entityName "CrudController"))
  35.                 ->setAction(Action::EDIT)
  36.                 ->setEntityId($entity->getId())
  37.                 ->generateUrl();
  38.             $event->setResponse(new RedirectResponse($url));
  39.         }
  40.     }
  41. }