Version 16, last updated by Gergo Erdosi at 23 Mar 22:16 UTC

There are two ways to use a plugin. You can use one of the established events, or establish you own event and dispatch it when appropriate. There reasons to use one or the other, but in general I would say that using established events is more useful unless the event has to be tied to the actions of the component.

We will create a plugin that has an established event, as well as a custom event. They are almost the same, but some minor differences exist. First we need to talk about the events available for plugins.

Nooku dynamically generates events around three main items: Dispatcher, Controller, and Database. Each of these items has numerous events. This is a full list of the basic events, each of the items has multiple actions, and a before and after call.

onBeforeDispatcher[Action], onAfterDispatcher[Action] Action can be: Dispatch, Forward, Render Authorize

onBeforeController[Action], onAfterController[Action] Action can be: Browse, Read, Edit, Add, Delete, any custom controller action

onBeforeDatabase[Action], onAfterDatabase[Action] Action can be: Select, Insert, Update, Delete

These clearly differ from the default Joomla events. Nooku uses a command chain pattern, meaning all processes are put into a chain before being run. This means you can influence the chain before it is run. There is one catch, the controller events will only work for Nooku extensions.

Nooku event plugin

Let's look at a simple example first.

<?php

class PlgKoowaMyapp extends PlgKoowaDefault
{
	public function onAfterControllerBrowse(KEvent $event)
	{
		print_r('You must be browsing');
	}
}

Now first we create a function with the name of the event. It will be automatically registered and run without any work on your part. This will just print out 'You must be browsing', which isn't that helpful. Let's look at an example that does something more productive.

<?php

class PlgKoowaMyapp extends PlgKoowaDefault
{
	public function onAfterControllerEdit(KEvent $event)
	{
		$caller = $event->caller; // Gives us the caller, the controller of the current request
		$result = $event->result; // Gives us the result, available for After events
		if (get_class($caller) == 'ComMyappControllerRecord') // Can check if the controller is the one we want
		$caller->verify($result); // If so, run a function in controller
	}
}

The beauty here is that we have access to the caller (and the result for After events). In this example you could take the result and verify the data, or whatever you need to do. The $event variable is a KEvent object holding details about the chain. You don't have to accept the $event variable if you don't want to.

Custom events

Creating your own events will look similar, but some differences exist. First, let's look at some code.

<?php

class PlgKoowaMyapp extends PlgKoowaDefault
{
	public function onDebugRecords($config = array())
	{
		// Create a config object.
		$config = new KConfig($config);

		// If we want to debug
		if ($config->debug == true)
		{
			// get our data
			$model = $this->getService('com://site/myapp.model.records');
			$records = $model->getList()->getData();	

			// Loop and print out details
			foreach ($records as $record)
			{
				print_r($record);
			}
		}
	}
}

This code will never execute on its own. Let's say you have a debug mode and want to print out the records. This code would do it, but you need to trigger it.

<?php

class ComMyappControllerRecords extends ComDefaultControllerDefault {

	public function  __construct(KConfig $config) {
		$this->getService('koowa:event.dispatcher')->dispatchEvent('onDebugRecords', array('debug' => true)); // Triggers the custom event
		parent::__construct($config);
	}
}

Now the records will print out whenever this controller is instantiated. You will notice that we aren't passing a KEvent object, but actually an associative array (or a KConfig object is acceptable). You can choose not to pass any arguments, just make sure to remove the parameter from the function definition in the plugin.

Enable the plugin

To enable the plugin: Save the file in /plugins/koowa/myapp.php; Add a row to jos_plugins, where the column element is set to the name of the plugin, in this example that's myapp, and where the column folder is set to koowa.

As with all in Nooku/Joomla naming your files and classes is very important. So keep in mind that the class should be PlgKoowaMyapp, the file should be myapp.php and the column element should be myapp as well, of course depending on what name you choose.