Creating a Custom Module for Joomla

Creating a Custom Module for Joomla

In this guide, we build a modern custom Joomla module from the ground up. Using the mod_hello example, we cover the project structure, manifest, service provider, Dispatcher, Helper, and display layout for Joomla 5 and 6.

Step 1: Project Structure

Start with the standard folder hierarchy for a modern Joomla module. Create the following structure inside mod_hello:

mod_hello/
|-- language/
|   +-- en-GB/
|   |   +-- mod_hello.ini
|   |   +-- mod_hello.sys.ini
|-- media/
|   +-- css/
|   +-- images/
|   +-- js/
|-- services/
|   +-- provider.php
|-- src/
|   +-- Dispatcher/
|   |   +-- Dispatcher.php
|   +-- Helper/
|       +-- HelloHelper.php
|-- tmpl/
|   +-- default.php
+-- mod_hello.xml

The language files hold interface strings and extension metadata. The media directory contains static assets, while services, src, and tmpl separate dependency registration, application logic, and presentation.

Step 2: The XML Manifest

The manifest is the module's installation definition. It lists files, declares the base namespace, and describes parameters such as a greeting, layout, and cache settings.

<?xml version="1.0" encoding="UTF-8"?>
<extension type="module" client="site" method="upgrade">
  <name>mod_hello</name>
  <author>Ponto Mega</author>
  <version>1.0.0</version>
  <description>MOD_HELLO_XML_DESCRIPTION</description>
  <namespace path="src">Joomla\Module\Hello</namespace>
  <files>
    <folder module="mod_hello">services</folder>
    <folder>src</folder>
    <folder>tmpl</folder>
  </files>
  <languages>
    <language tag="en-GB">language/en-GB/mod_hello.ini</language>
    <language tag="en-GB">language/en-GB/mod_hello.sys.ini</language>
  </languages>
  <config>
    <fields name="params">
      <fieldset name="basic">
        <field name="greeting" type="text" default="Hello, World!" />
      </fieldset>
    </fields>
  </config>
</extension>

Step 3: The Service Provider

services/provider.php is the modern entry point. It registers the module's Dispatcher and Helper factories with Joomla's dependency-injection container, so the framework can construct these classes without legacy require_once calls.

<?php
\defined('_JEXEC') or die;

use Joomla\CMS\Extension\Service\Provider\HelperFactory;
use Joomla\CMS\Extension\Service\Provider\Module;
use Joomla\CMS\Extension\Service\Provider\ModuleDispatcherFactory;
use Joomla\DI\Container;
use Joomla\DI\ServiceProviderInterface;

return new class () implements ServiceProviderInterface {
    public function register(Container $container)
    {
        $container->registerServiceProvider(new ModuleDispatcherFactory('\\Joomla\\Module\\Hello'));
        $container->registerServiceProvider(new HelperFactory('\\Joomla\\Module\\Hello\\Site\\Helper'));
        $container->registerServiceProvider(new Module());
    }
};

Step 4: The Dispatcher

The Dispatcher acts as the controller. Joomla invokes it when rendering the module; it obtains the Helper, retrieves the required data, and adds that data to the layout context.

class Dispatcher extends AbstractModuleDispatcher implements HelperFactoryAwareInterface
{
    use HelperFactoryAwareTrait;

    protected function getLayoutData()
    {
        $data = parent::getLayoutData();
        $data['list'] = $this->getHelperFactory()
            ->getHelper('HelloHelper')
            ->getItems($data['params'], $this->getApplication());
        return $data;
    }
}

Step 5: The Helper

The Helper provides the data-access layer. In this example it uses Joomla's database services to retrieve records, keeping database work out of the template and making the module easier to maintain.

class HelloHelper implements DatabaseAwareInterface
{
    use DatabaseAwareTrait;

    public function getItems(Registry $params, SiteApplication $app)
    {
        $query = $this->getDatabase()->getQuery(true)
            ->select('a.id, a.title')
            ->from('#__modules AS a')
            ->order('a.title ASC')
            ->setLimit($params->get('count', 5));
        return $this->getDatabase()->setQuery($query)->loadObjectList();
    }
}

Step 6: The Display Layout

Keep the template focused on presentation. The Web Asset Manager loads the module's CSS and JavaScript, and the template renders the configured greeting and returned items.

<?php
$wa = $app->getDocument()->getWebAssetManager();
$wa->registerAndUseStyle('mod_hello_css', 'media/mod_hello/css/style.css');
$wa->registerAndUseScript('mod_hello_script', 'media/mod_hello/js/script.js');
if (!$list) {
    return;
}
?>
<div class="hello">
  <h3><?php echo $params->get('greeting'); ?></h3>
  <ul>
    <?php foreach ($list as $item) : ?>
      <li><?php echo $item->title; ?></li>
    <?php endforeach; ?>
  </ul>
</div>

Share this knowledge:

Direct Engineering

Have a Joomla project or technical challenge in mind?

From architectural diagnosis to production deployment, directly with the engineer.