Jump to content
  • 0

Plugin - Getevents Method


MartyIX

Question

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(-)");     
     }
}
Link to comment
Share on other sites

3 answers to this question

Recommended Posts

  • 0

The developer manual is very brief in this regard. It would definitely help others. 

 

Could you please confirm that whether I did the event registration in triggerEvent function above correctly or not? The thing is that sometime I need to adjust Blesta and events represent the least invasive way how to that.

Link to comment
Share on other sites

  • 0

The Events object is already loaded and available to AppController, so no need to load it.

 

public function triggerEvent() {
          \BlestaLogger::addDebug("plugins/yourPluginName/controllers/InvoiceTester::triggerEvent()");
        $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(-)");     
}
 
// in /components/events/default/event_callback.php unless you've loaded it prior to $this->Events->trigger()
class EventCallback {
    public static function triggerPluginEvent(EventObject $event) {
 
    }
}
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...