You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 11 Next »

Some plugins may need to be invoked to perform certain tasks immediately before or after an action has occurred. For this reason plugins may register plugin events. These Events automatically initialize the plugin and invoke the desired event handler (i.e. callback method) when triggered. All event handlers accept a single EventObject parameter.

For performing tasks at regular intervals see Plugin Cron Tasks.

Event class change

When implementing this you should not check the EventObject class because it has been deprecated and will be replaced by EventInterface in v5.0.0.

Adding Plugin Events

To add plugin events, simply return them from your plugin's getEvents() method.

/plugins/my_plugin/my_plugin_plugin.php
<?php
class MyPluginPlugin extends Plugin {

	...

	public function getEvents() {
		return array(
			array(
				'event' => "Appcontroller.preAction",
				'callback' => array("this", "run")
			)
			// Add multiple events here
		);
	}

	public function run($event) {
		// This will execute when the "Appcontroller.preAction" event is triggered
	}
}
?>

In the example above you'll notice the callback is referencing a class named "this". Blesta automatically maps the string "this" to the plugin when the event is triggered. This is because we want the instance of the class loaded at the time of the event to be triggered, not the instance at the time the event was registered. You can specify here any valid callback, though it is best to use "this" or static class methods where possible as there is limited space to store class instances. 

 

  • No labels