Wednesday 21 March 2018

Developing a basic joomla plugin






Joomla is one of the flexible CMS that enables users to create a website with minimal effort. For developers, this would be a cake walk to setup joomla.  However, if you are looking to develop extension for Joomla, then you might want to consider few tips and follow some predefined set of rules which we are going to discuss in this article.
A joomla extension  basically consists of 4 basic files that are required to develop a plugin. Let's know about each file while we develop a basic hello world plugin.

The naming convention of a extension basically includes the extension type followed by the name of the extension separated by underscore('_').
Let's name our plugin as basic. Since, we are developing a module type extension, I am naming the plugin as 'mod_basic'. So our file structure would be something like below:

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

  1. mod_basic.php:
    This file is the main file for a joomla module which is first called by joomla to initialize a module. This file includes helper.php and calls helper's class method which is responsible to execute the given query and retrieve data. Later, it includes the template of the module which is default.php that is responsible to display the data.

Here's our code for mod_basic.php

// No direct access without joomla
defined('_JEXEC') or die;
 
// Include the syndicate functions only once
require_once dirname(__FILE__) . '/helper.php';

$hello = modBasicHelper::getHello($params);
require JModuleHelper::getLayoutPath('mod_basic');
 


  2.   helper.php
   
    This file can be considered as the brain of the module because it includes the instructions that enables the module to retrieve the data. In this file, we create module's class and it's methods to process and retrieve the data. This file includes code that basically does the 'communicate with database and retrieve data' part.

Here's our code for helper.php. If you notice the code below, it defines the "ModBasicHelper" class mentioned in mod_basic.php

class ModBasicHelper
{
    /**
     * Retrieves the hello message
     *
     * @param   array  $params An object containing the module parameters
     *
     * @access public
     */
    public static function getHello($params)
    {
        return 'Hello, World! I am a basic module';
    }
}
 
3. default.php

    This file includes the template of the module that displays the module's content. This includes the HTML structure to display the data provided by mod_basic.php file. The main module's file includes the template file with the JModuleHelper::getLayoutPath method, which first will check for any template overrides.

Place this file in a folder named "tmpl".

Here we are just returning "hello world" message in default.php file

<?php
// No direct access
defined('_JEXEC') or die; ?>
<?php echo $hello; ?>

4. mod_basic.xml

 This xml file can be called as guide for the module as it helps joomla understand about the module structure during installation. It specifies the files that will be copied by the installer, and also contains the information about the parameters of the module that are used by the module manager, as well additional information about the module.
The type parameter includes values like modules, plugins while client includes administrator and site which means module access levels to be designed for admin or site user.

Here's the xml file for our module:

<?xml version="1.0" encoding="utf-8"?>
<extension type="module" version="3.1.0" client="administrator" 
 method="upgrade">
    <name> Module - Hello World!</name>
    <author>Sravan</author>
    <version>1.0.0</version>
    <description>A simple Hello, World! module.</description>
    <files>
        <filename>mod_basic.xml</filename>
        <filename module="mod_basic">mod_basic.php</filename>
        <filename>helper.php</filename>
        <filename>tmpl/default.php</filename>
    </files>
    <config>
    </config>
</extension>


We are done with the basic Joomla module. Zip these files in to mod_basic.zip file which is our module installer file.

Considering that you have already setup joomla, login to your joomla and follow  the below steps to install the module:

1. On the top menu, click on Manage > Extensions > Install
 
2. Click on "Upload From Package" option and select browse to upload the above zip file.




3. Joomla installer will install this module and shows "installation successful" message. Click on "Extensions > Modules" which lists all the modules. From the drow down filter under "New" option select "Adminstrator" since we  have  developed the module for administrator.



4. Click on your module which you named in <filename> tag in xml file. In module settings, change the "position" to home page by typing cpanel(select Isis). This decides the position of your module. You may also place it in other places of your Joomla admin page. Here I am placing it in the control panel of admin.
Change the status to "published" and select "save & close" option.



5. Now the module setup is ready, you may check your module by clicking on home(joomla icon on the top left) which shows up your custom module.









Similarly, you may customize the code in helper file to fetch the articles and other content from database which can be displayed in your module.

Hope this was helpful!

Happy learning!