Write your own Plugin
Submitted by Hagen Graf on
Thank you for your contribution:
A plugin is a kind of Joomla! extension.
The plug-in becomes active when a predefined event occurs. An event could occur e.g. when the event onContentPrepare happens. That means while Joomla! prepares the content to be displayed our plug-in add something to the preparations. Think of the core plug-in pagebreak. If the events is fired, the plug-in get's active. If it finds the pattern <hr class="system-pagebreak" /> in the text, it will implement the pagebreak.
Joomla! has eight plug-in types: authentication, content, editors-xtd, editors, extension, search, system and user. These are also the names of the sub directories where the plug-in files are located. For example, plug-ins with a type of authentication are located in the directory plugins/authentication. A plug-in has to be installed via the extension manager.
Joomla provides for every type of plugin predefined events, e.g the content events
- onAfterDisplay
- onAfterContentSave
- onAfterDisplayTitle
- onAfterDisplayContent
- onPrepareContent
- onBeforeDisplay
- onBeforeContentSave
- onBeforeDisplayContent
- onContentPrepareForm
- onContentPrepareData
You find all the existing events in the Joomla! plug-in documentation.
Every extension can define its own events and this allow other extensions to respond to their events and make extensions extensible (Figure 1).
Figure 2: Plug-In, Component
Example
To show a very easy example, we want to display a textstring above the article text (Figure 2).
Figure 2: Example Plug-In
To implement our task we have to write a content plug-in that I called cocoateaddtext. We only need two files, the .xml file with the metadata (Listing 1) and a php file for our code (Listing 2).
<?php
defined('_JEXEC') or die;
jimport('joomla.plugin.plugin');
class plgContentCocoateAddText extends JPlugin
{
public function onContentPrepare( $context, &$article, &$params, $limitstart=0 )
{
$article->text = "<strong>My special text</strong>".$article->text ;
return true;
}
}
Listing 1: /plugins/content/cocoateaddtext/cocoateaddtext.php
<?xml version="1.0" encoding="utf-8"?>
<extension version="1.7" type="plugin" group="content">
<name>PLG_CONTENT_COCOATEADDTEXT</name>
<author>Hagen Graf</author>
<creationDate>Dec 2011</creationDate>
<copyright> :) </copyright>
<license>GNU General Public License version 2 or later; see LICENSE.txt</license>
<authorEmail>info@cocoate.com</authorEmail>
<authorUrl>www.cocoate.com</authorUrl>
<version>1.0</version>
<description>PLG_CONTENT_COCOATEADDTEXT_XML_DESCRIPTION</description>
<files>
<filename plugin="cocoateaddtext">cocoateaddtext.php</filename>
<filename>index.html</filename>
</files>
</extension
Listing 2: /plugins/content/cocoateaddtext/cocoateaddtext.xml
After creating these files you have to "discover" and install the plug-in - Extensions -> Extension-Manager -> Discover (Figure 3)
Figure 3: Discover and install the plug-in
After activating in the plug-in manager your frontpage articles will look like the ones in the screenshot in Figure 2.
| Attachment | Size |
|---|---|
| 1.75 KB |


























Recent comments