Search code examples
symfonyswiftmailersonata-user-bundlesonata

Symfony2, Sonata, UserBundle : Send email when user is enable on update


I would like to be able to send an email to the user when the admin enable the user in Sonata admin panel.

enter image description here

I have to idea which file i should override or extend.

I suppose it is similar to this subject or this stack-overflow subject:

/**
 * {@inheritdoc}
 */
public function create($object)
{
    parent::create($object);

    // send welcome email to new user
}

I found the create function in the admin bundle but i suppose it would not be overridden only for the UserBundle. If it is indeed the way to go, how could i specify for the UserBundle only?

For the information I can't find any create or update function in UserBundle. Only in the AdminBundle.


Solution

  • I will relate to contract event from my gist: https://gist.github.com/webdevilopers/4eea317ade72a119a72e Adapt it to your needs. Guess you can simply rename "Contract" to "User".

    Then add the event somewhere in your admin class:

    $event = new ContractEvent($contract);
    
    $dispatcher = $this->get('event_dispatcher');
    $dispatcher->dispatch(
        ContractEvents::CONTRACT_CREATED,
    $event
    ); 
    

    See the gist for details how to inject SwiftMailer.

    Instead of creating your own event you can choose from the events Sonata Admin offers you: Admin's documentation - Reference - Events (master)

    Pick up the one that suits your needs.