EasyAdminBundle: EasyAdmin event is not working

Describe the bug Hello, I have created my event and subscribed it! But still it couldn’t work! I don’t know what i have missed in it!

I have followed this tutorials to create this source code!

i.e https://knpuniversity.com/screencast/easyadminbundle/event-hooks

I am using sf3.4 and Easyadmin 1.16

Here is my source code

services.yaml

services:
    AdminBundle\Event\EasyAdminSubscriber:
        tags:
            - { name: kernel.event_listener, event: kernel.exception }
    _defaults:
        autowire: true
        autoconfigure: true
        public: false
        
    AdminBundle\:
        resource: 'AdminBundle/*'
        exclude: 'AdminBundle/{Entity,Repository,Tests,Event}'
        
    AdminBundle\Controller\:
        resource: 'AdminBundle/Controller'
        public: true
        tags: ['controller.service_arguments']

EventSubscriber.php

namespace AdminBundle\Event;
use UserBundle\Entity\User;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;


class EasyAdminSubscriber implements EventSubscriberInterface {

    private $user;

    public function __construct(User $user) {
        
        $this->user = $user;
    }

    public static function getSubscribedEvents() {
        return [
            EasyAdminEvents::PRE_UPDATE => 'onPreUpdate',
        ];
    }

    public function onPreUpdate(GenericEvent $event) {
        
        $id = $this->request->query->get('id');
        $entity = $event->em->getRepository('UserBundle:User')->find($id);
        //$entity = $this->em->getRepository('UserBundle:User')->find($id);

        if ($entity instanceof User) {
            //$user = $this->tokenStorage->getToken()->getUser();
            if (!$user instanceof User) {
                $user = null;
            }

            $email = $entity->getEmail();
            $nom = $entity->getNom();
            $prenom = $entity->getPrenom();
            $civilite = $entity->getCivilite();
            $userStatut = $entity->getUserStatut()->getId();
            $email_sujet = "Votre compte d'utilisation";

            $ee['email'] = $email;
            $ee['sujet'] = $email_sujet;


            if (in_array('ROLE_INV', $roles)) {
                $ee['templ'] = $this->renderView('email/registrationIV.html.twig', array('civilite' => $civilite, 'nom' => $nom, 'prenom' => $prenom, 'email' => $email));
                $this->sendEmail($ee);
            } elseif (in_array('ROLE_PRO', $roles)) {
                $ee['templ'] = $this->renderView('email/accessP.html.twig', array('civilite' => $civilite, 'nom' => $nom, 'prenom' => $prenom, 'email' => $email));
                $this->sendEmail($ee);
            } elseif (in_array('ROLE_CLU', $roles)) {
                $ee['templ'] = $this->renderView('email/accessC.html.twig', array('civilite' => $civilite, 'nom' => $nom, 'prenom' => $prenom, 'email' => $email));
                $this->sendEmail($ee);
            } elseif (in_array('ROLE_ADM', $roles)) {
                $ee['templ'] = $this->renderView('email/accessA.html.twig', array('civilite' => $civilite, 'nom' => $nom, 'prenom' => $prenom, 'email' => $email));
                $this->sendEmail($ee);
            } else {
                $ee['templ'] = $this->renderView('email/registrationI.html.twig', array('civilite' => $civilite, 'nom' => $nom, 'prenom' => $prenom, 'email' => $email, 'telephone' => $telephone));
                $this->sendEmail($ee);
            }

            return $this->redirectToRoute('easyadmin', [
                        'action' => 'list',
                        'entity' => $this->request->query->get('entity'),
            ]);
        }
    } 
}

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 21

Most upvoted comments

Can you please explain from which of the above files this was taken from (i.e. where is it stored in your project)?