Home About Us Developer Blogs Joomla Development Adding Plugin triggers in your extensions. How & Why..
Adding Plugin triggers in your extensions. How & Why..
Tuesday, 08 March 2011 08:41

Why have plugin triggers ?

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 orther 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.

Use of Plugin triggers when hacking/ modifying 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 , its 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 !

Parth Lawate

 

Adding Plugin Triggers in Joomla Extensions

By Shantanu ( Tekdi Web Solutions & Techjoomla)

Hey Everyone !

This is my first attempt at a technical blog.. So do excuse any mistakes you find. Comments & Feedback is very much welcome !

Lets 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 shall use to call trigger.  Let us now look at the various methods these classes offer ...

Class: JPluginHelper
Method of class: importPlugin()
Loads all the plugin files for a particular type. If no specific plugin is specified otherwise only the specific pugin is loaded.It returns true if success.
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
string $plugin

Class: JDispatcher
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..

Lets take the example of Docman...

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..

 JPluginHelper::importPlugin('docman');
$dispatcher =& JDispatcher::getInstance();
$dispatcher->trigger('OnAfterDocumentSave', array() )

 

Simple, isnt 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..


Shatanu
Written on Tuesday, 08 March 2011 08:41 by Shatanu

Viewed 2301 times so far.
Like this? Tweet it to your followers!
Tagged underJoomlaplugin triggers

Rate this article

(3 votes)

Latest articles from Shatanu

Latest 'tweets' from Techjoomla.

  • The Techjoomla. Daily is out! http://t.co/UA2zf5dc ▸ Top stories today via @mandville @absalomedia @joomlablogger Link Monday, 21 May 2012 10:33
  • @cozimek rocking #joomla Saving the world while building a #joomla business http://t.co/JcfpvlnP Link Sunday, 20 May 2012 15:09
  • AHA0 WOW from #cb looks pretty awesome ! @joomlapolis #jab12 Link Sunday, 20 May 2012 13:34
  • Use #joomla and payment gateways in your extensions ? Come talk to us #jab12 we'll make ur job dead easy ! Cc : @techjoomla Link Sunday, 20 May 2012 11:58
  • The Techjoomla. Daily is out! http://t.co/UA2zf5dc ▸ Top stories today via @hagengraf @HilsCheyne @Arlen Link Sunday, 20 May 2012 10:33
blog comments powered by Disqus

 

Newsletter Signup

Jump on to our Newsletter & stay updated on News on our Extensions, New Releases & Offers !

porno porno porno porno teen porn videos teen porn movies amatuer porn videos