The ability to add Plugin Triggers in your Joomla extensions, is a great asset to developers which is not always used to the fullest extent.. Having various Plugin events in your extension means that you give other developers the ability to extend your extension, or modify it to suit their requirements without having to hack core files. The same logic that is used by Joomla content plugins or JomSocial plugins etc can also be applied to your extensions.
At Tekdi Web Solutions we do a lot of Joomla Service & get client needs to modify extensions left & right. Where possible this is always done using Plugins & overrides. But the time comes when a hack has to be made. To reduce the impact, instead of modifying the whole extension, we just add plugin triggers where needed & usually send back the modifications to the original developer where possible. This serves two purposes. One is that the code maintenance is way easier & its very easy to document & identify these small hacks. Secondly, there is always a chance that if the main dev accepts your patch, these "hacks" might become regularised in the future. Even if they aren't, it's still much easier to re-patch the extension with plugin triggers.
So in order to share this knowledge, I asked one of our Developers, Shantanu to write a small blog about how you can go about adding triggers. Shantanu is just one of the new bloggers I shall be introducing to this blog in the next few weeks. So stay tuned for more!
Over to Shantanu for the same! Read on to read the full blog!
Adding Plugin Triggers in Joomla Extensions
By Shantanu ( Tekdi Web Solutions & Techjoomla)
This is my first attempt at a technical blog. So do excuse any mistakes you find. Comments & Feedback is very much welcome!
Let's start with some simple methods that are used to add plugin triggers. We shall be using the "JPluginHelper" class in order to load the plugins we need & we shall also be needing the "JDispatcher" class that will use to call trigger. Let us now look at the various methods these classes offer ...
Method of class: importPlugin()
Loads all the plugin files for a particular type. If no specific plugin is specified otherwise only the specific plugin is loaded. It returns true if successful.
Syntax: boolean importPlugin (string $type, [string $plugin = null], [ $autocreate = true], [ $dispatcher = null])
Where : string $type - The plugin type, relates to the sub-directory in the plugins directory
Method of class: getInstance()
Returns a reference to the global Event Dispatcher object, only creating it if it doesn't already exist.
Method of class: trigger()
Triggers an event by dispatching arguments to all observers that handle the event and returning their return values.
Syntax: array trigger (string $event, [array $args = null], [boolean $doUnpublished = false])
Where : string $event - The event name
array $args - An array of arguments
Now that this is covered, lets look at examples where you can see it in action..
We had a situation where one of our Tekdi Web Solutions clients needed to save additional fields against Docman documents. The issue was the Docman has no plugin triggers that would allow us to do this without a hack. So what we did was we added a new Plugin trigger called as "OnAfterDocumentSave" which allowed us to capture the extra field data in the POST array. The fields were added via a template override. The key is to know where to place the plugin trigger. In our case, it was in Docman's save function.
Once the position was identified & verified, all we had to do was push the below lines of code in the right place.
$dispatcher =& JDispatcher::getInstance();
$dispatcher->trigger('OnAfterDocumentSave', array() );
Simple, isn't it? So make sure that you add plenty of useful plugin events when you make your next extension. We are already doing this for all Techjoomla extensions in a phased manner so that integration with other extensions, as well as extending them without hacks will be easier.
When you subscribe to the blog, we will send you an e-mail when there are new updates on the site so you wouldn't miss them.