How to Develop a Custom Joomla Module?

We have spoken and wrote about many articles on Joomla and its importance on website development. Now for better understanding of Joomla, here we present an article on How to develop a custom Joomla Module. For better introduction, we can take an image slider module creation that we had developed as an example.

The basic form of Joomla 1.5 consist of two files:
XML file
PHP file

XML file has general information about the module as shown in administration Interface of Joomla module manager and also module parameters that helps to modify the appearance/functionality of the module. The PHP file affords controlling logic to the module as per our requirement.
The Basic files required to create a Joomla custom (Image Slider) module are given below
• index.html
• module_name.xml (Eg. mod_simpleslider.xml )
• module_name.php (Eg. mod_simpleslider.php)
Note: It is very important, that XML file name must matches module name. Otherwise, installer will install the module, but Joomla wouldn’t show parameters and additional information stored in XML.

Index file format:
index.html
This helps to ensure that a default page is displayed without listing all of the other files in the directory incase of unauthorized users attempted to access the directory. It’s not necessary, but a good practice.

<html><body background=”#fff”></body></html>

XML file format:
mod_simpleslider.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<install type=”module” version=”1.5.0″>
// Name of the Module
<name>SimpleSlider Module</name>
// Name of the Author
<author>treeshore.com</author>
//Version Month & year of the Module
<creationDate>December 2011</creationDate>
//Copyright information
<copyright>All rights reserved to TreeShore.com.</copyright>
//License Information
<license>GPL 2.0</license>
//Author’s email address
<authorEmail>[email protected]</authorEmail>
// Author’s website
<authorUrl>www.treeshore.com</authorUrl>
//Module version number
<version>1.0.0</version>
//Description of what the module does
<description>SimpleSlider – This module will show image slideshow created with Simpleslider</description>
// Listing of all files that should be installed for the module to function – in our case the file structure will be…
<files>
<filename module=”mod_simpleslider”>mod_simpleslider.php</filename>
<filename>index.html</filename>
<folder>tmpl/</folder>
<folder>engine/</folder>
<folder>data/</folder>
</files>
//Optional parameters
<params>
</params>
//in our case
<params group=”advanced”>
<param name=”cache” type=”list” default=”1″ label=”Caching” description=”Select whether to cache the content of this module”>
<option value=”1″>Use global</option>
<option value=”0″>No caching</option>
</param>
<param name=”cache_time” type=”text” default=”900″ label=”Cache Time” description=”The time before the module is recached” />
</params>
</install>

The Installer copy and install the files as defined by the above xml file. Notice that we DO NOT include a reference in the files section for the XML file.

PHP File Format
mod_simpleslider.php:
The basic structure of a module PHP file format will be looks like the following:

<?php 

//don’t allow other scripts to grab and execute our file
defined(‘_JEXEC’) or die(‘Direct Access to this location is not allowed.’);
?>

In our case: the php file will be:-

<?phpaddStyleSheet(JURI::base() . ‘modules/mod_’.$module->name.’/engine/style.css’);
//by default to retrieve a style sheet, the format will be
//$document->addStyleSheet(JURI::base() . ‘modules/mod_’.$module->name.’$your url to stylsheet directory’);

$document->addScript(JURI::base() . ‘modules/mod_’.$module->name.’/engine/jquery.js’);
//by default to retrieve a js file, the format will be
//$document->addScript(JURI::base() . ‘modules/mod_’.$module->name.’$your javascript/jquery directoy url’);
$document->addScript(JURI::base() . ‘modules/mod_’.$module->name.’/engine/wowslider.js’);
require(JModuleHelper::getLayoutPath(‘mod_’.$module->name));
?>

When the above mod_simpleslider.php file is loaded in Joomla by means of PHP include directive it retrieves the stylesheet & javascript files from its source directory and produces the slider output to the final page.
To package this module for distribution and installation, simply zip the files together: Eg. mod_simpleslider.zip.
The resulting mod_simpleslider.zip file may be uploaded and installed through the standard Joomla! 1.5 Extension Manager.