Joomla - Creating Modules

In this chapter, we will learn how to create modules in Joomla. Modules are flexible, lightweight, and useful page rendering extensions.
Create Modules
Following are the simple steps to create modules in Joomla.
Step 1 − Create a folder called mod_firstmodule in Joomla → modules folder .
Step 2 − In the mod_firstmodule folder, create a file called "helper.php". This file contains the class name as a helper, it helps to display the received data in the output of the module.
helper.php
<? php /** * Helper class for Hello World! module * * @package Joomla.Tutorials * @subpackageModules * @link http://docs.joomla.org/J3.x:Creating_a_simple_module/Developing_a_Basic_Module * @license GNU/GPL, see LICENSE.php * mod_helloworld is free software. 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. */ class ModHelloWorldHelper { /** * Retrieves the hello message * * @param array $params An object containing the module parameters * * @access public */ public static function getHello ( $params ) { return 'Hello, World!' ; } } ?>
Step 3 - Create a file called mod_helloworld.php . This is the entry point for the module, which performs the initialization procedures, collects the necessary data, and renders the output of the module using a template.
mod_helloworld.php
<? php /** * Hello World! Module Entry Point * * @package Joomla.Tutorials * @subpackageModules * @license GNU/GPL, see LICENSE.php * @link http://docs.joomla.org/J3.x:Creating_a_simple_module/Developing_a_Basic_Module * mod_helloworld is free software. 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 ; // Include the syndicate functions only once require_once dirname ( __FILE__ ) . '/helper.php' ; $hello = modHelloWorldHelper :: getHello ( $params ); require JModuleHelper :: getLayoutPath ( 'mod_helloworld' ); ?>
Step 4 - Create mod_helloworld.xml file . This file contains information about the module. This XML file contains information about the files that should be installed in Joomla for the module.
mod_helloworld.xml file
<? xml version = "1.0" encoding = "utf-8" ?> <extension type = "module" version = "3.1.0" client = "site" method = "upgrade" > <name> Hello World! </name> <author> Tutorials Point </author> <version> 1.0.0 </version> <description> A simple Hello World! module. </description> <files> <filename> mod_helloworld.xml </filename> <filename module = "mod_helloworld" > mod_helloworld.php </filename> <filename> index.html </filename> <filename> helper.php </filename> <filename > tmpl/default.php </filename> <filename> tmpl/index.html </filename> </files> <config> </config> </extension>
Step 5 − Create a simple HTML file named index.html . The purpose of writing this file is that the created directories should not be browsed. When the user browses these directories, the index.html file is displayed. You can even leave this file empty.
index.html
<html> <body> Welcome to Tutorials Point!!!!! </body> </html>
Step 6 − Create a folder named tmpl . Place the default.php file as shown below and the index.html (created in step (5)) in the tmpl folder . The default.php file is a template that displays the module's output.
default.php
<?php /** * @package Joomla.Site * @subpackage mod_firstmodule * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved. * @license GNU General Public License version 2 or later; see LICENSE.txt */ defined('_JEXEC') or die; > <p>Hello World!!!!!!</p>
After you have finished creating all these files, compress the complete mod_firstmodule folder .
Step 7 − Go to Extension → Extension Manager in Joomla Admin and you will get the following screen. Here you can download and install the module files you created, i.e. mod_firstmodule folder . Click Choose File and select the generated module file (compressed). Click the Download and Install button to download the module file.
Step 8 − Once downloaded and installed, go to Module Manager and click on New . There you can view the module file you created as shown below.
Step 9 − You can assign this module similar to other modules and then publish it.