Catching events triggered

Re: Catching events triggered

by Camille Tardy -
Number of replies: 4
Picture of Plugin developers

Thanks Paul,


I saw them on the event 2 page but their description is really vague and I couldn’t get what they were meant to do. 

Anyway thanks for the tip! 


Camille

In reply to Camille Tardy

Re: Catching events triggered

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

If you create a file called 'events.php' in the '/db' subfolder of your plugin and fill it in as described in the docs (with the name of the event and the name of the callback function) and bump your plugin version number (to get Moodle to recognise the changes you have made). Then, when the given event is triggered, the function you specified is called, with the event object as the parameter.

e.g. if you have an events.php file containing:

$observers = array(
    array(
        'eventname' => '\core\event\user_created',
        'callback' => '\local_myplugin\event_handler::user_created',
    ),
);

Then, in your plugin's '/classes' subfolder, you create a file 'event_handler.php' and fill it in with:

namespace local_myplugin;

class event_handler {
    public static function user_created(\core\event\user_created $event) {
        // Do something with this event, probably making use of $event->userid.
    }
}

Then your 'user_created' function will be called immediately after any new user account is created.

Average of ratings: Useful (3)
In reply to Davo Smith

Re: Catching events triggered

by Iñaki Arenaza -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Hi Camille,

for a full (and very simple) example that expands on Davo's answer, you can have a look at my "Notify site administrators about new Email Signups" plugin (https://moodle.org/plugins/local_notifyemailsignup). You can browse the code at https://github.com/iarenaza/moodle-local-notifyemailsignup

Saludos. Iñaki.

Average of ratings: Useful (1)