How To Create Module For Joomla 3.x

Modules are lightweight and flexible extensions that are displayed in pages at different module positions. These modules are displayed like boxes arranged around a component in top, sidebar or footer position. For example, login module or html module.

Each module has a single entry point located in the modules directory:

mod_modulename/mod_modulename.php

This file has three functions. It includes helper file, call helper functions to get required data, and finally includes the template that will display the module output.

Hello World Module

Let's create a simple Joomla module that will display Hello World.

There are four basic files in module:

  1. mod_helloworld.php
  2. mod_helloworld.xml
  3. helper.php
  4. tmpl/default.php

mod_helloworld.php: This file is the 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_helloworld.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.

helper.php: This file contains the helper class which is used to do the actual work in retrieving the information to be displayed in the module, from the database or some other source.

tmpl/default.php: This is the module template. This file will take the data collected by mod_helloworld.php and generate the HTML to be displayed on the page.

1. mod_helloworld.php

This file will perform three tasks:

  1. Include the helper.php file which contains the class to be used to collect the necessary data
  2. Invoke the appropriate helper class method to retrieve the data
  3. Include the template to display the output

Task 1

A simple line to include helper.php file using require_once statement. This file is used to do all database queries and prepare the data for the view.

require_once dirname(__FILE__) . '/helper.php';

require_once is used because helper functions are defined within a class, and we only want the class defined once.

Task 2

Then, you need to load the data from the helper and put it into an array. Helper class will contain one method: getHello(). The helper class method is invoked in the following way:

$hello = modHelloWorldHelper::getHello($params);

Task 3

The last line of code tells the module to load the default layout (tmpl/default.php) for the view. Finally, template file is included in the following way:

require JModuleHelper::getLayoutPath('mod_helloworld');

2. helper.php

This file contains the helper class that is used to retrieve the data to be displayed in the module output. In this example, helper class will have one method: getHello(). This method will return the ‘Hello, World’ message.

class ModHelloWorldHelper
{
public static function getHello($params)
{
return 'Hello, World!';
}
}

3. tmpl/default.php

In Joomla, the content is separated from the presentation, so you need to create a view file to display the content. The view should not contain any database queries, you only want to use the view to present the content. This allows a user to potentially create a template override of this view and modify the output without having any clue about how the rest of the module works.

This file is the template which displays the module output. This can be done in following way:

echo $hello;

The template file has the same scope as the mod_helloworld.php file. It means that the variable $hello can be defined in the mod_helloworld.php file and then used in the template file without any extra declarations or function calls.

Subscribe for Free Updates

Please enable the javascript to submit this form