Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

ActionEasyAdminSubscriber.php 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace Lc\SovBundle\EventSubscriber\Action;
  3. use Doctrine\ORM\PersistentCollection;
  4. use Doctrine\ORM\Query;
  5. use EasyCorp\Bundle\EasyAdminBundle\Event\AfterCrudActionEvent;
  6. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeCrudActionEvent;
  7. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent;
  8. use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
  9. use Gedmo\Translatable\TranslatableListener;
  10. use Lc\SovBundle\Doctrine\Extension\TranslatableInterface;
  11. use Lc\SovBundle\Doctrine\EntityManager;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use function Symfony\Component\Translation\t;
  14. class ActionEasyAdminSubscriber implements EventSubscriberInterface
  15. {
  16. protected $em;
  17. protected $adminUrlGenerator;
  18. public function __construct(EntityManager $entityManager, AdminUrlGenerator $adminUrlGenerator)
  19. {
  20. $this->em = $entityManager;
  21. $this->adminUrlGenerator = $adminUrlGenerator;
  22. }
  23. public static function getSubscribedEvents()
  24. {
  25. return [
  26. AfterCrudActionEvent::class => ['overrideSortAction'],
  27. ];
  28. }
  29. public function overrideSortAction(AfterCrudActionEvent $event)
  30. {
  31. $actions = $event->getResponseParameters()->get('global_actions');
  32. if ($actions) {
  33. foreach ($actions as $i=>$action) {
  34. //récriture du bouton 'retour au parent'
  35. if ($action->getName() == 'index_parent') {
  36. $entity = $event->getAdminContext()->getEntity()->getInstance();
  37. if ($entity !== null) {
  38. if($entity->getParent() !==null){
  39. $url = $this->adminUrlGenerator
  40. ->setController($event->getAdminContext()->getCrud()->getControllerFqcn())
  41. ->set('entityId', $entity->getParent()->getId())
  42. ->generateUrl();
  43. $action->setLinkUrl($url);
  44. }
  45. }else{
  46. unset($actions[$i]);
  47. }
  48. }
  49. if ($action->getName() == 'sort') {
  50. $entityId = $event->getAdminContext()->getRequest()->get('entityId');
  51. if ($entityId != null) {
  52. $url = $this->adminUrlGenerator
  53. ->setController($event->getAdminContext()->getCrud()->getControllerFqcn())
  54. ->setAction($action->getName())
  55. ->set('entityId', $entityId)
  56. ->generateUrl();
  57. $action->setLinkUrl($url);
  58. }
  59. }
  60. }
  61. }
  62. }
  63. }