Skip to content

7. Joomla Module Development

Definitions

  • Joomla Component: Components are the building blocks of Joomla! and are the most common type of extension. Components are used to display content, manage data, and interact with users. https://docs.joomla.org/Component
  • Joomla Module: Modules are lightweight and flexible extensions used for page rendering. In Joomla, Module = Widget = A block of code that can be placed in a template position. https://docs.joomla.org/Module
  • Joomla Plugin: Plugins are small extensions that can be used to extend the functionality of Joomla. https://docs.joomla.org/Plugin
  • Joomla Template: Templates are the visual presentation of a Joomla site. https://docs.joomla.org/Template

Map WordPress to Joomla

WordPress Joomla
Plugin Plugin
Widget Module
Theme Template
Post Article
Page Article
  • Joomla components are broad term for a group of extensions. It is a collection of modules, plugins, templates, and libraries.
  • Joomla extensions are also broadly classified into 4 types: components, modules, plugins, and templates.
  • Template contains multiple Components; Each Component contains one or more Modules or Plugins. A Module can also include one or more Plugins or just be a static HTML block.

Creating a Joomla Module 1

  • Steps to create a joomla module:
    1. Create a new folder, setup the manifest file, and create the module file.
    2. Edit the module source code.
    3. Zip the module folder and install it in Joomla.
    4. Configure the module in Joomla, and add it to a template position.
  • Variables available to you while developing a Joomla module:
    • $module: The module object.
    • $params: The module parameters.
    • $attribs: The module attributes.
    • $app: The application object.
    • $lang: The language object.
    • $template: The template object.
    • $path: The path to the module.
    • $scope: The scope of the module.
    • Super Globals: The super globals are available to you as well, like: $_SERVER, $_GET, $_POST, $_FILES, $_COOKIE, $_SESSION, $_REQUEST, $_ENV and $_GLOBALS.
  • It is important not to override the listed variables, as they are used by Joomla.
  • It is important not to use the same name for the module folder and the module file. For example, if you name the module file mod_hello.php, then the module folder should be named mod_hello and not hello.
  • mod_ is the prefix for all module files.
  • tmpl_ is the prefix for all template files.
  • plg_ is the prefix for all plugin files.
  • com_ is the prefix for all component files.
  • manifest.xml is the manifest file for all extensions, and it is located in the root of the extension folder. It is used to define the extension name, version, description, author, etc. It is also used to define the extension dependencies, and the extension installation and uninstallation scripts.
  • Details of the manifest file https://docs.joomla.org/Special:MyLanguage/Manifest_files
<?xml version="1.0" encoding="utf-8"?>
<extension type="module" version="3.1.0" client="site" method="upgrade">
    <name>Hello, World!</name>
    <author>John Doe</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>
  • script.php is the installation and uninstallation script for all extensions. It is located in the root of the extension folder. It is used to define the installation and uninstallation scripts for the extension.

Dealing with DB from inside the Module

  • In the manifest file, You can define the sql to be executed when the module is installed/uninstalled/upgraded or its config updated.
<install>
     <sql>
         <file driver="mysql" charset="utf8">sql/mysql/install.mysql.utf8.sql</file>
         <file driver="sqlazure" charset="utf8">sql/sqlazure/install.sqlazure.utf8.sql</file>
     </sql>
</install>

<uninstall>
     <sql>
         <file driver="mysql" charset="utf8">sql/mysql/uninstall.mysql.utf8.sql</file>
         <file driver="sqlazure" charset="utf8">sql/sqlazure/uninstall.sqlazure.utf8.sql</file>
     </sql>
</uninstall>

<update>
    <schemas>
        <schemapath type="mysql">sql/mysql/updates</schemapath>
    <schemapath type="sqlazure">sql/sqlazure/updates</schemapath>
    </schemas>
</update>
...
<files>
    ....
    <folder>sql</folder>
</files>
  • You can put any sql queries in the sql files. For example, you can create a table in the ./sql/install.mysql.utf8.sql file, and drop the table in the ./sql/uninstall.mysql.utf8.sql file.
  • You can use the Joomla Database API to interact with the database from inside the module.
<?php
function modHelloWorldHelper($params)
{
    // Obtain a database connection
    $db = JFactory::getDbo();
    // Retrieve the shout
    $query = $db->getQuery(true)
                ->select($db->quoteName('hello'))
                ->from($db->quoteName('#__helloworld'))
                ->where('lang = ' . $db->Quote('en-GB'));
    // Prepare the query
    $db->setQuery($query);
    // Load the row.
    $result = $db->loadResult();
    // Return the Hello
    return $result;
}

Configuring the Module

  • Use FormField to create the form fields for the module configuration.
  • Inside the manifest file, you can define the form fields for the module configuration.
...
<config>
    <fields name="params">
        <fieldset name="basic">
            <field
                name="lang"
                type="sql"
                default="1"
                label="Select a language"
                query="SELECT id AS value, lang FROM #__helloworld" />
        </fieldset>
    </fields>
</config>
  • The above code allows you to select a language from the #__helloworld table, and will add lang to the $params object.
  • The lang field will appear in the module configuration page (in the back-end) as a drop-down list.
  • The field will be saved in the db #__modules table, in the params column, as a JSON string.
  • You can access the field value in the module file as $params->get('lang').
  • More about FormFields here: https://docs.joomla.org/Special:MyLanguage/Form_field

Preview Template Positions

  • Go to System > Global Configuration > Templates, and enable Preview Module Positions option.
  • Go to your front-end website, and add ?tp=1 to the end of the URL. All positions in the template will be highlighted.
  • See here: https://youtu.be/pgFR09vs6KU?t=480

References