J1.5

Difference between revisions of "Creating a Plugin for Joomla"

From Joomla! Documentation

m (Adjusted link)
(Updated contents (based on the contents of the 1.5.0 release package))
Line 4: Line 4:
  
 
=== Creating the Installation File ===
 
=== 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.
+
As with all extensions 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.
 
<source lang="xml">
 
<source lang="xml">
 
<?xml version="1.0" encoding="iso-8859-1"?>
 
<?xml version="1.0" encoding="iso-8859-1"?>
Line 28: Line 28:
 
As you can see, the system is similar to other Joomla! XML installation files. You only have to look out for the <code>group="xxx"</code> entry in the <code><install></code> tag and the extended information in the <code><filename></code> tag. This information tells Joomla! into which folder to copy the file and to which group the plugin should be added.
 
As you can see, the system is similar to other Joomla! XML installation files. You only have to look out for the <code>group="xxx"</code> entry in the <code><install></code> tag and the extended information in the <code><filename></code> tag. This information tells Joomla! into which folder to copy the file and to which group the plugin should be added.
  
If you are creating a plugin that responds to existing core events, the <code>group="xxx"</code> attribute would be changed to reflect the name of existing plugin folder for the event type you wish to augment. e.g. <code>group="authentication"</code> or <code>group="user"</code>. See [[Plugin]] for a complete list of existing core event categories. In creating a new plugin to respond to core events it is important that your plugin's name is unique and does not conflict with any of the other plugins that may also be responding to the core event you wish to service as well.
+
If you are creating a plugin that responds to existing core events, the <code>group="xxx"</code> attribute would be changed to reflect the name of existing plugin folder for the event type you wish to augment. e.g. <code>group="authentication"</code> or <code>group="user"</code>. See [[Plugin/Events]] for a complete list of existing core event categories. In creating a new plugin to respond to core events it is important that your plugin's name is unique and does not conflict with any of the other plugins that may also be responding to the core event you wish to service as well.
  
 
If you are creating a plugin to respond to non-core system events your choice for the <code>group="xxx"</code> tag should be different than any of the existing core categories.
 
If you are creating a plugin to respond to non-core system events your choice for the <code>group="xxx"</code> tag should be different than any of the existing core categories.
Line 35: Line 35:
  
 
=== Creating the Plugin ===
 
=== Creating the Plugin ===
Joomla! 1.5 introduces a new, more object-orientated, way of writing plugins. Though older plugins will work in legacy mode, using the old method is strongly discouraged for new projects.
+
Joomla! 1.5 introduces a new, more object-oriented way of writing plugins. Though older plugins will work in legacy mode, using the old method is strongly discouraged for new projects.
 +
 
 +
The object-oriented way of writing plugins involves writing a subclass of [[JPlugin]], a base class that implements the basic properties of plugins. In your methods, the following properties are available:
 +
 
 +
* <code>$this->params</code>: the [[Parameter|parameters]] set for this plugin by the administrator
 +
* <code>$this->_name</code>: the name of the plugin
 +
* <code>$this->_type</code>: the group (type) of the plugin
 +
 
 +
In the following code example, <code><PluginGroup></code> represents the group (type) of the plugin, and <code><PluginName></code> represents its name. Note that class and function names in PHP are case-insensitive.
 +
 
 +
{{notice|The code example previously used here contained a deprecated method of retrieving the plugin's [[Parameter|parameters]]. The current code example displays a faster and cleaner method.}}
  
 
<source lang="php">
 
<source lang="php">
Line 50: Line 60:
 
  * Constructor
 
  * Constructor
 
  *
 
  *
  * For php4 compatability we must not use the __constructor as a constructor for
+
  * For php4 compatibility we must not use the __constructor as a constructor for
 
  * plugins because func_get_args ( void ) returns a copy of all passed arguments
 
  * plugins because func_get_args ( void ) returns a copy of all passed arguments
 
  * NOT references.  This causes problems with cross-referencing necessary for the
 
  * NOT references.  This causes problems with cross-referencing necessary for the
 
  * observer design pattern.
 
  * observer design pattern.
 
  */
 
  */
  function plg<PluginGroup><PluginName>( &$subject )
+
  function plg<PluginGroup><PluginName>( &$subject, $config )
 
  {
 
  {
     parent::__construct( $subject );
+
     parent::__construct( $subject, $config );
  
    // load plugin parameters
 
    $this->_plugin = JPluginHelper::getPlugin( '<GroupName>', '<PluginName>' );
 
    $this->_params = new JParameter( $this->_plugin->params );
 
 
  }
 
  }
 
/**
 
/**
Line 71: Line 78:
  
 
         // Plugin code goes here.
 
         // Plugin code goes here.
 +
        // You can access parameters via $this->params.
 
     return true;
 
     return true;
 
  }
 
  }

Revision as of 15:06, 11 February 2012

The "J1.5" namespace is an archived namespace. This page contains information for a Joomla! version which is no longer supported. It exists only as a historical reference, it will not be improved and its content may be incomplete and/or contain broken links.

The plugin structure for Joomla! 1.5 is 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.

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[edit]

As with all extensions 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.

<?xml version="1.0" encoding="iso-8859-1"?>
<install version="1.5" type="plugin" group="search">
   <name>Categories searchbot</name>
   <author>Joomla! Project</author>
   <creationDate>November 2005</creationDate>
   <copyright>(C) 2005 Open Source Matters. All rights reserved.</copyright>
   <license>GNU/GPL</license>
   <authorEmail>admin@joomla.org</authorEmail>
   <authorUrl>www.joomla.org</authorUrl>
   <version>1.1</version>
   <description>Allows searching of Categories information</description>
   <files>
       <filename plugin="categories.searchbot">categories.searchbot.php</filename>
   </files>
   <params>
       <param name="search_limit" type="text" size="5" default="50" label="Search Limit" description="Number of search items to return"/>        
   </params>
</install>

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 the <install> tag and the extended information in the <filename> tag. This information tells Joomla! into which folder to copy the file and to which group the plugin should be added.

If you are creating a plugin that responds to existing core events, the group="xxx" attribute would be changed to reflect the name of existing plugin folder for the event type you wish to augment. e.g. group="authentication" or group="user". See Plugin/Events for a complete list of existing core event categories. In creating a new plugin to respond to core events it is important that your plugin's name is unique and does not conflict with any of the other plugins that may also be responding to the core event you wish to service as well.

If you are creating a plugin to respond to non-core system events your choice for the group="xxx" tag should be different than any of the existing core categories.

Tip If you add the attribute method="upgrade" to the tag install, this plugin can be installed without uninstalling an earlier version. All existing files will be overwritten, but old files will not be deleted.

Creating the Plugin[edit]

Joomla! 1.5 introduces a new, more object-oriented way of writing plugins. Though older plugins will work in legacy mode, using the old method is strongly discouraged for new projects.

The object-oriented way of writing plugins involves writing a subclass of JPlugin, a base class that implements the basic properties of plugins. In your methods, the following properties are available:

  • $this->params: the parameters set for this plugin by the administrator
  • $this->_name: the name of the plugin
  • $this->_type: the group (type) of the plugin

In the following code example, <PluginGroup> represents the group (type) of the plugin, and <PluginName> represents its name. Note that class and function names in PHP are case-insensitive.

Info non-talk.png
General Information

The code example previously used here contained a deprecated method of retrieving the plugin's parameters. The current code example displays a faster and cleaner method.

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

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

class plg<PluginGroup><PluginName> extends JPlugin
{
/**
 * Constructor
 *
 * For php4 compatibility 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<PluginGroup><PluginName>( &$subject, $config )
 {
    parent::__construct( $subject, $config );

 }
/**
 * Plugin method with the same name as the event will be called automatically.
 */
 function <EventName>()
 {
    $app = &JFactory::getApplication();

        // Plugin code goes here.
        // You can access parameters via $this->params.
    return true;
 }
}
?>

Using Plugins in Your Code[edit]

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 that case you don't need to do the following.

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

$dispatcher =& JDispatcher::getInstance();
$results = $dispatcher->trigger( '<EventName>', <ParameterArray> );

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

If you are creating a plugin for a new, non-core event, remember to activate your plugin after you install it. Precede any reference to your new plugin with the JPluginHelper::importPlugin() command.