joomla
  1. joomla-create-modules

Create Modules - Joomla Modules

Modules are a key feature of Joomla that allow you to add functionality to your website. In this article, we'll explore how to create custom modules in Joomla.

Syntax

The syntax for creating custom modules in Joomla is as follows:

  1. Create a new module folder within the /modules directory.
  2. Create a new PHP file for the module.
  3. Define the module class and extend the JModuleHelper class.
  4. Create the mod_<modulename>.xml file to define the module parameters.

Example

Here is an example of creating a custom module in Joomla:

  1. Create a new folder named "my_module" within the /modules directory.
  2. Create a new PHP file called "mod_my_module.php" within the /my_module directory.
defined('_JEXEC') or die;

use Joomla\CMS\HTML\HTMLHelper;

class mod_my_module extends JModuleHelper
{
    public function __construct(&$params)
    {
        parent::__construct($params);
    }

    public function renderModule($module)
    {
        $output = '';

        // Display the module title
        if ($module->showtitle)
        {
            $output .= '<h2>' . $module->title . '</h2>';
        }

        // Display the module content
        $output .= '<div>' . $module->content . '</div>';

        return $output;
    }
}
  1. Create the XML file named "mod_my_module.xml" within the /my_module directory.
<?xml version="1.0" encoding="utf-8"?>
<extension type="module" version="3.0" client="site" method="upgrade">
    <name>My Module</name>
    <author>My Company</author>
    <version>1.0.0</version>
    <description>My custom module for Joomla</description>
    <files>
        <filename module="mod_my_module">mod_my_module.php</filename>
        <filename>index.html</filename>
    </files>
    <config>
        <fields name="params">
            <fieldset name="basic">
                <field name="showtitle" type="radio" default="1" label="Show Title" description="Select whether to show the module title or not">
                    <option value="1">Yes</option>
                    <option value="0">No</option>
                </field>
            </fieldset>
        </fields>
    </config>
</extension>

Output

The resulting module will display the module title and content based on the module parameters.

Explanation

Modules are a key feature of Joomla that allow you to add functionality to your website. Custom modules can be created by creating a new folder within the /modules directory, adding a new PHP file for the module, defining the module class and extending the JModuleHelper class, and creating the mod_<modulename>.xml file to define the module parameters.

Use

Custom modules can be used to add functionality to your Joomla website, such as displaying custom content, images, and forms. They can also be used to enhance the user experience by providing additional information and interactivity.

Important Points

  • Custom modules can be created by creating a new folder, adding a new PHP file, defining the module class, and creating the mod_<modulename>.xml file.
  • Modules can be used to add functionality to your Joomla website.
  • Custom modules can enhance the user experience by providing additional information and interactivity.

Summary

In this article, we explored how to create custom modules in Joomla. We discussed the syntax for creating custom modules, an example of creating a custom module, the resulting output, the explanation of custom modules, their uses, and important points. Modules are an essential feature of Joomla that allow you to add functionality to your website. Custom modules can be used to display custom content, images, and forms, and enhance the user experience by providing additional information and interactivity.

Published on: