From Joomla! Documentation
Revision as of 16:29, 17 September 2012 by Cadrlp
Creating MW Module for Joomla 2.5 with Mehta Websolution
A module is a lightweight and flexible extension that is used for page rendering. They are used for small bits of the page that are generally less complex and are able to be seen across different components.
You can see many examples of modules in the standard Joomla! install: - menus - Latest News - Login form - and many more.
This tutorial will explain how to go about creating a simple Hello World module. Through this tutorial you will learn the basic file structure of a module. This basic structure can then be expanded to produce more elaborate modules.
There are four basic files that are used in the standard pattern of module development:
- mod_mw_okie_dude.php - This file is the main entry point for the module. It will perform any necessary initialization routines, call helper routines to collect any necessary data, and include the template which will display the module output.
- mod_mw_okie_dude.xml - This file contains information about the module. It defines the files that need to be installed by the Joomla! installer and specifies configuration parameters for the module.
- tmpl/default.php - This is the module template. This file will take the data collected by mod_mw_okie_dude.php and generate the HTML to be displayed on the page.
The complete mod_mw_okie_dude.php file is as follows:
<?php /** * @module MW Okie Dude - Joomla 2.5 Module * @author Nirav Mehta ( Mehta Websolution ) * @companyname Mehta Websolution * @link http://mehtawebsolution.com * @package Mehta Websolution - Joomla Tutorials * @subpackage Modules * @link http://dev.joomla.org/component/option,com_jd-wiki/Itemid,31/id,tutorials:modules/ * @license GNU/GPL, see LICENSE.php * mod_mw_okie_dude is free software. developed by www.mehtawebsolution.com * This version may have been modified pursuant. * to the GNU General Public License, and as distributed it includes or * is derivative of works licensed under the GNU General Public License or * other free or open source software licenses. */ // no direct access defined( '_JEXEC' ) or die; require JModuleHelper::getLayoutPath('mod_mw_okie_dude'); ?>
The one line that we haven’t explained so far is the first line. This line checks to make sure that this file is being included from the Joomla! application. This is necessary to prevent variable injection and other potential security concerns.
The default.php file is the template which displays the module output.
The code for the default.php file is as follows:
<?php // no direct access defined( '_JEXEC' ) or die; ?> <p><?php echo $params->get('mw_okie_dude_message'); ?></p>
An important point to note is that the template file has the same scope as the mod_mw_okie_dude.php file. What this means is that the variable $message can be defined in the mod_mw_okie_dude.php file and then used in the template file without any extra declarations or function calls.
The mod_mw_okie_dude.xml is used to specify which files the installer needs to copy and is used by the Module Manager to determine which parameters are used to configure the module. Other information about the module is also specified in this file.
The code for mod_mw_okie_dude.xml is as follows:
<?xml version="1.0" encoding="utf-8"?> <extension type="module" version="2.5.0" client="site" method="install"> <name>MW Okie Dude</name> <author>Nirav Mehta</author> <creationDate>April 2012</creationDate> <copyright>Copyright (C)2012 Mehta Websolution</copyright> <license>GNU General Public License version 2 or later</license> <authorEmail>firstname.lastname@example.org</authorEmail> <authorUrl>www.mehtawebsolution.com</authorUrl> <version>1.0</version> <description>A module listing all of the activities developed by Mehta Websolution (http://mehtawebsolution.com)</description> <files> <filename module="mod_mw_okie_dude">mod_mw_okie_dude.php</filename> <filename>mod_mw_okie_dude.xml</filename> <filename>css/index.html</filename> <filename>css/mw_okie_dude.css</filename> // here you can put your CSS code or Add your owned CSS file name // <filename>images/index.html</filename> <filename>js/mw_okie_dude.js</filename> // here you can put your JS(Jquery) code or Add your owned JS(Jquery) file name // <filename>js/index.html</filename> <filename>tmpl/index.html</filename> <filename>tmpl/default.php</filename> <filename>index.html</filename> </files> <config> <fields name="params"> <fieldset name="basic"> <field name="mw_okie_dude_message" type="text" default="MW Okie Dude" label="Message" description="Message to display above activity list" /> </fieldset> </fields> </config> </extension>
You will notice that there are five additional files that we have not yet mentioned: index.html, css/index.html, images/index.html, js/index.html, tmpl/index.html. These files are included so that these directories cannot be browsed. If a user attempts to point their browser to these folders, the index.html file will be displayed. These files can be left empty or can contain the simple line:
which will display an empty page.
Since our module does not use any parameters, this section is empty.
Module development for Joomla2.5 . Using the techniques described in this tutorial, an endless variety of modules can be developed with Ravi Mehta.
Company : Mehta Websolution | Link : http://mehtawebsolution.com | Authors : Nirav Mehta & Ravi Mehta | City : Jamnagar | Country : India