General developer forum

Issues getting event listeners working

 
Picture of Erik Fox
Issues getting event listeners working
 

I am new to moodle development as well as php. I am trying to get some event listeners working in moodle 3.5. At this point I am just trying to get some output in the console when an event is triggered. My plugin is in the form of a block. Here is my file structure:

Here is the code in events.php:

defined('MOODLE_INTERNAL') || die();
$observers = array (
array (
'eventname' => '\core\event\user_loggedin',
'callback' => 'block_simplehtml_observer::update',
),
array (
'eventname' => '\core\event\user_loggedout',
'callback' => 'block_simplehtml_observer::update',
),
array (
'eventname' => '\core\event\course_module_created',
'callback' => 'block_simplehtml_observer::update',
),
);

Here is the code in observer.php

defined('MOODLE_INTERNAL') || die();
class block_simplehtml_observer {
public static function update(\core\event\base $event) {
// This code works when put inside the corresponding 'store' function in blocks/recent_activity/classes/observer.php
$json_data = json_encode($event->get_data());
echo("<script>console.log('EVENT: ".$json_data."');</script>");
}
}

The rest of the files directly follow the simplehtml block tutorial on moodles development site. I am not sure whether there is something that I need to do in another file to get this working or I am doing something incorrectly in these two files. I have been trying to debug this for a couple of hours by looking at existing events/observer files with no luck. Any help would be much appreciated.

 
Average of ratings: -
Davo
Re: Issues getting event listeners working
Core developersParticularly helpful MoodlersPlugin developers

Have you bumped the version number in version.php? Until you do that, Moodle will not parse your db/events.php file to find any new observers.

You do not appear to be using automatic class loading to load the class that handles the events (I'd expect the observer function to be called '\block_simplehtml\observer::update' in the events.php, and the file to be stored in classes/observer.php, with: "namespace \block_simplehtml;" (without quotes) at the top of it). If you're not using automatic class loading, you'll need to specify the name of the file that contains the handler code, via an 'includefile' entry in the capabilities array.

I'd also strongly recommend against adding echo statements to observers - the event is usually triggered before any page output is started, so adding echo statements will almost certainly break the HTML of the page and will also break any redirect() statements found after the event is triggered.


 
Average of ratings: Useful (1)
Picture of Erik Fox
Re: Issues getting event listeners working
 

Bumping the version number worked. I guess I wasn't aware that things in the db folder are stored in the cache. I am actually using automatic class loading and that is working perfectly fine. The echo statements are temporary and just a way for me to easily see that my code is working and what information comes with the events until I start displaying data in the actual block. Thank you for the help!

 
Average of ratings: -