I'm trying to add a new event to getEvents() method in my plugin. To my surprise the method is not called. I suspect that Blesta caches the data somewhere as a part of a plugin installation process.
Am I right? Or am I missing something?
<?php
class MyPluginPlugin extends Plugin {
...
public function getEvents() {
// THIS METHOD IS NOT CALLED. I CHECKED BY ADDING A LOGGING STATEMENT HERE.
return array(
array(
'event' => "Appcontroller.preAction",
'callback' => array("this", "run")
),
array(
'event' => "Another.event",
'callback' => array("this", "anotherEvent")
)
);
}
public function run($event) {
// This will execute when the "Appcontroller.preAction" event is triggered
}
}
?>
public function getEvents() {
return array(
array(
'event' => "Invoices.add",
'callback' => array("this", "invoiceAdded")
)
);
}
public function invoiceAdded($event)
{
\BlestaLogger::addDebug("plugins/my_plugin_plugin/Plugin::invoiceAdded(event)", [$event]);
\BlestaLogger::addDebug("plugins/my_plugin_plugin/Plugin::invoiceAdded(): TODO - call a method in NettAccounts model");
\BlestaLogger::addDebug("plugins/my_plugin_plugin/Plugin::invoiceAdded(-)");
}
Question
MartyIX
Hola,
I'm trying to add a new event to getEvents() method in my plugin. To my surprise the method is not called. I suspect that Blesta caches the data somewhere as a part of a plugin installation process.
Am I right? Or am I missing something?
<?php class MyPluginPlugin extends Plugin { ... public function getEvents() { // THIS METHOD IS NOT CALLED. I CHECKED BY ADDING A LOGGING STATEMENT HERE. return array( array( 'event' => "Appcontroller.preAction", 'callback' => array("this", "run") ), array( 'event' => "Another.event", 'callback' => array("this", "anotherEvent") ) ); } public function run($event) { // This will execute when the "Appcontroller.preAction" event is triggered } } ?>Thanks!
EDIT: I tried my idea with installation of the plugin and the method was really called. So that seems to be the culprit.
EDIT2: I could have just have a look at plugin_manager.php file...
EDIT3: This seems to work:
Event registration in /plugins/my_plugin_name/my_plugin_plugin.php (see http://docs.blesta.com/display/dev/Plugin+Events):
public function getEvents() { return array( array( 'event' => "Invoices.add", 'callback' => array("this", "invoiceAdded") ) ); } public function invoiceAdded($event) { \BlestaLogger::addDebug("plugins/my_plugin_plugin/Plugin::invoiceAdded(event)", [$event]); \BlestaLogger::addDebug("plugins/my_plugin_plugin/Plugin::invoiceAdded(): TODO - call a method in NettAccounts model"); \BlestaLogger::addDebug("plugins/my_plugin_plugin/Plugin::invoiceAdded(-)"); }Sample controller /plugins/my_plugin_name/Controllers/InvoiceTester.php:
class InvoiceTester extends AppController { /** * @link https://shop.example.com/plugin/yourPluginName/InvoiceTester/triggerEvent/ * @return void */ public function triggerEvent() { \BlestaLogger::addDebug("plugins/yourPluginName/controllers/InvoiceTester::triggerEvent()"); Loader::loadComponents($this, array("Events")); $this->Events->register("Invoices.add", array("EventCallback", "triggerPluginEvent")); $event = new \EventObject("Invoices.add", array('foo' => 'bar')); $this->Events->trigger($event); \BlestaLogger::addDebug("plugins/yourPluginName/controllers/InvoiceTester::triggerEvent(-)"); } }3 answers to this question
Recommended Posts
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now