Creating the Plugin - Joomla

by satheesh 2010-02-04 09:03:55

How to create your own plugin

This How-To should provide you with the basics of what you need to know to develop your own plugin. Most plugins consist of just a single code file but to correctly install the plugin code it must be packaged into an installation file which can be processed by the Joomla! installer.

Creating the installation file

As with all add-ons in Joomla!, plugins are easily installed as a .zip file (.tar.gz is also supported) but a correctly formatted XML file must be included. As an example, here is the XML installation file for the categories searchbot plugin.




Categories searchbot
Joomla! Project
November 2005
(C) 2005 Open Source Matters. All rights reserved.
GNU/GPL
admin@joomla.org
www.joomla.org
1.1
Allows searching of Categories information

categories.searchbot.php






As you can see, the system is similar to other Joomla! XML installation files. You only have to look out for the group="xxx" entry in thetag and the extended information in thetag. This information tells Joomla! into which folder to copy the file and to which group the plugin should be added.


Creating the Plugin

Joomla! 1.5 introduces a new, more object-orientated, way of writing plugins. The old method is still supported for backwards-compatibility (see next section).


// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

// Import library dependencies
jimport('joomla.event.plugin');

class plg extends JPlugin
{
/**
* Constructor
*
* For php4 compatability we must not use the __constructor as a constructor for
* plugins because func_get_args ( void ) returns a copy of all passed arguments
* NOT references. This causes problems with cross-referencing necessary for the
* observer design pattern.
*/
function plg( &$subject )
{
parent::__construct( $subject );

// load plugin parameters
$this->_plugin = > JPluginHelper::getPlugin( '', '' );
$this->_params = new JParameter( $this->_plugin->params );
}

/**
* Plugin method with the same name as the event will be called automatically.
*/
function ()
{
global $mainframe;

// Plugin code goes here.

return true;
}

}
?>

Creating the Plugin (Legacy Mode)

This section describes the legacy method of creating plugins used prior to Joomla! 1.5. Some core Joomla! plugins may still use this method but will be gradually re-written over time. This method is still supported for backwards-compatibility.

The code that you want executed when the event triggers is written as a PHP function. Prior to the function definition you make a call to the registerEvent() method so that the Joomla! event system associates your function with the appropriate event.

For example, take a look at this skeleton code:


// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

$mainframe->registerEvent( '', '' );

function ( ) {

//Plugin code goes here

}
?>

With the function $mainframe→registerEvent() your plugin is registered into the Joomla! event system. This means that when the event called '' is later triggered, the function called '' will be called.

Now you can write your plugin function code anyway you want. If you want to use parameters, it's no problem, just use them as usual. You can register as many events and plugin functions together in one file as you want. When you're done, your plugin is ready for use.

Using Plugins in Your Code

Now that you've created your plugin, you will probably want to call it in your code. You might not; the Joomla! core has a number of built-in events that you might want your plugin code to be registered to. In which case you don't need to do the following.

If you want to trigger an event then you use code like this:


$results = $mainframe->triggerEvent( '', );

It is important to note that the parameters have to be in an array. The plugin function itself will get the parameters as single values. The return value will consist of an array of return values from the different plugins (so it can also contain multilevel arrays).

Conclusion

The plugin structure for Joomla! 1.5 is very flexible and powerful. Not only can plugins be used to handle events triggered by the core application and extensions, but plugins can also be used to make third party extensions extensible and powerful.
2034
like
0
dislike
0
mail
flag

You must LOGIN to add comments