<?php
# src/EventSubscriber/EasyAdminSubscriber.php
namespace App\EventSubscriber;
use App\Controller\Admin\ProductCrudController;
use App\Entity\Product;
use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeCrudActionEvent;
use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent;
use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
class EasyAdminSubscriber implements EventSubscriberInterface
{
private $adminUrlGenerator;
public function __construct(AdminUrlGenerator $adminUrlGenerator)
{
$this->adminUrlGenerator = $adminUrlGenerator;
}
public static function getSubscribedEvents()
{
return [
BeforeCrudActionEvent::class => ['redirectDetail'],
];
}
public function redirectDetail(BeforeCrudActionEvent $event)
{
$entity = $event->getAdminContext()->getEntity()->getInstance();
if ($event->getAdminContext()->getCrud()->getCurrentAction() == "detail") {
$entityName = $event->getAdminContext()->getEntity()->getFqcn();
$entityName = explode("\\", $entityName);
$entityName = $entityName[2];
$url = $this->adminUrlGenerator
->setController(("App\Controller\Admin\\" . $entityName . "CrudController"))
->setAction(Action::EDIT)
->setEntityId($entity->getId())
->generateUrl();
$event->setResponse(new RedirectResponse($url));
}
}
}