Easyadminbundle: Easyadmin subscriber events

Created on 22 Mar 2018  路  3Comments  路  Source: EasyCorp/EasyAdminBundle

Hello,

Symfony Framework : 3.2

Easyadmin : ^1.16

My event is'nt working correctly did i missed anything?

EasyAdminSubscriber.php

<?php

namespace AdminBundle\EventListener;

use EasyCorp\Bundle\EasyAdminBundle\Event\EasyAdminEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
use UserBundle\Entity\User;

class EasyAdminSubscriber implements EventSubscriberInterface {


    public static function getSubscribedEvents() {
        return array(
            EasyAdminEvents::POST_EDIT => array('postUpdate'),
        );
    }

    public function postUpdate(GenericEvent $event) {

        $entity = $event->getSubject();

        if (!($entity instanceof User)) {
            return;
        }


        $id = $entity->getId();
        //$user_entity = $this->em->getRepository('UserBundle:User')->find($id);

        $email = $entity->getEmail();
        $nom = $entity->getNom();
        $prenom = $user_entity->getPrenom();
        //$sujet = $user_entity->getPrenom();
        $civilite = $entity->getCivilite();
        $userStatut = $entity->getUserStatut()->getId();     


        if (!$entity->hasRole('ROLE_EMPLOYEE') ) {

            if($entity->addRole('ROLE_EMPLOYEE')) {
                $ee['sujet'] = 'Your account on our site';
                $ee['templ'] = $this->renderView('email/inscriptionComplete.html.twig', array('nom' => $nom, 'prenom' => $prenom, 'civilite' => $civilite));
                $ee['email'] = $email;
                $this->sendEmail($ee);
            }
        }
    }

    private function sendEmail($ee) {
        #Mailer Configuration
        $mailer_transport = $this->container->getParameter('mailer_transport');
        $mailer_host = $this->container->getParameter('mailer_host');
        $mailer_user = $this->container->getParameter('mailer_user');
        $mailer_port = $this->container->getParameter('mailer_port');
        $mailer_password = $this->container->getParameter('mailer_password');

        $smtp = new \Swift_SmtpTransport($mailer_host, $mailer_port, 'ssl');
        $smtp->setUsername($mailer_user);
        $smtp->setPassword($mailer_password);

        $mailer = new \Swift_Mailer($smtp);
        $full_email = \Swift_Message::newInstance($smtp)
                ->setSubject($ee['sujet'])
                ->setFrom($mailer_user)
                ->setBcc($ee['email'])
                ->setBody($ee['templ'], 'text/html');
        return $mailer->send($full_email);
    }
}

AdminController.php

use EasyCorp\Bundle\EasyAdminBundle\Controller\AdminController as BaseAdminController;
use AdminBundle\EventListener\EasyAdminSubscriber;
use Symfony\Component\EventDispatcher\EventDispatcher;

class AdminController extends BaseAdminController {

    /** @var array The full configuration of the entire backend */
    protected $config;

    /** @var array The full configuration of the current entity */
    protected $entity;

    /** @var Request The instance of the current Symfony request */
    protected $request;

    /** @var EntityManager The Doctrine entity manager for the current entity */
    protected $em;

    public function __construct() {
        $dispatcher = new EventDispatcher();
        $subscriber = new EasyAdminSubscriber();
        $dispatcher->addSubscriber($subscriber);
    }

Services.yml

services:
    app.easy_admin.send_email:
        class: AdminBundle\EventListener\EasyAdminSubscriber
        #arguments: ['@role']
        tags:
            - { name: kernel.event_subscriber, event: easy_admin.post_update, method: postUpdate }   
Waiting feedback bug

Most helpful comment

There are some issues in your code. First, you should remove the code of the constructor of your AdminController. There's no need to created the dispatcher, subscriber, etc.

Second, if you use autowiring, you can remove the config in services.yml. If you are not using autowiring, you need to use this config instead:

services:
    app.easy_admin.send_email:
        class: AdminBundle\EventListener\EasyAdminSubscriber
        tags:
            - { name: kernel.event_subscriber }

All 3 comments

There are some issues in your code. First, you should remove the code of the constructor of your AdminController. There's no need to created the dispatcher, subscriber, etc.

Second, if you use autowiring, you can remove the config in services.yml. If you are not using autowiring, you need to use this config instead:

services:
    app.easy_admin.send_email:
        class: AdminBundle\EventListener\EasyAdminSubscriber
        tags:
            - { name: kernel.event_subscriber }

There is no email alert! But the role is changed succesfully!

Let's close this because it looks like a Symfony issue and not specifically related to this bundle. Please reopen if you can provide a reproducer of this bug as explained here. Thanks!

Was this page helpful?
0 / 5 - 0 ratings