Before knowing how to create a module in Joomla step by step from the backend you should know the Joomla module.
What is the Module of Joomla?
Sometimes we need to display a small block of information like the newsflash, Login Form, Slider, etc. So in Joomla, this type of block is called a Module. The Joomla module is paced according to the positions of the template.
Create a Module in Joomla step by step
In this tutorial, I will show how you can create a custom module for Joomla3.x and Joomla4.x.
Please follow the following steps –
Step1: Create directory structure with mod_latestposts
There are two ways to create a directory in your Joomla website module folder or in your computer’s local folder.
If you will create it on your local computer then you will need to create a zip file and install it by the Joomla Extension manager.
Now I am starting from the Joomla website root directory and then selecting the Modules folder.
Let’s create a module: mod_latestposts
Note: Module root folder name must be started from “mod_”(eg. mod_modulename) also the main file must be the same naming convention.
- Create inside the module directory of your Joomla website the following folders:
- /public_html/modules/mod_ latestposts /tmpl
- /public_html/modules/mod_ latestposts /language
- /public_html/modules/mod_ latestposts /language/en-GB
The above directories must be followed the same naming convention as this important rule.
Let’s See: How do I enable Joomla PHP error reporting?
Step2: Create file mod_ latestposts.php
This is the main file of the module and its name same as the directory name mod_latestposts.php inside the mod_latestposts.
The defined directive limits permit the rest of the file or code from any other non-Joomla scripts. In other words, if it’s not from Joomla standard code or out of the Joomla directory, It will “die” or stop all further action.
JLoader::register- This function loads the file that is included in the path. For the example above loader, function have helper.php
JLoader::register(‘ModPostsLatestHelper’, __DIR__ . ‘/helper.php’);
You can use require_once() in the place of loader ::register() for example as below-
But the latest version of Joomla is using the Loader() function.
Step3: Create file mod_ latestposts.xml
Above the screenshot, there is some important fields ex. Name, Author, creationDate, etc
To add files in our module you need to add these fields: <filename></filename>
And to add folders use this tag <folder></folder>. And these all fields tags should be inside the <files></files> fieldset.
To add a language file you need to use <languages> directives and inside this use <language></language> directives like the below –
The section includes language files and a language folder(ex. en-GB). When you will install the module these language files will be placed within the language folder.
For your module, you can also add a custom language folder. The benefit of a custom folder is that it will not be removed by updates, and your module will be self-contained.
Configuration settings start from the config tag, and inside this tag, we can add multiple fieldsets like Basic, Advanced, etc.
In the basic field set, we add parameters for the module function, and the Advanced field set is used for the default options of the Joomla module.
Step #4. Create Module helper.php
In this file you have to create a helper.php file with a helper class for example, in our module helper class is – ModPostsLatestHelper
You have to use the Camel case for a naming convention for the class name.
In fact, the helper.php file isn’t required to run a module. If you browse the other module folders, you’ll notice that several of them are missing. However, for this example, we’ll need to write one.
What is the use of this helper.php?
You included a line requiring the helper.php file when you will write the mod_latestposts.php file. Actually, we write database action methods in this file.
If you include this, but you don’t have it, Joomla is going to give you an error message that the file wasn’t found. If you don’t want to use one, don’t use the need to require in the mod_latestposts.php file.
Helper file methods – in the helper.php file you have to write your function or methods are you will need to get data from database tables. In our module, I created the getposts() method to get the latest posts from the content table.
Step #5. Check all files and folders –
Now we can install the module from the administrator. So first you have to go to administrator and click on the System menu on the left side. There you can see in the middlebox Install .where you can see the Discover link and click on it.
After that you will see module mod_latestposts there, just click on the Install button, and then it will install in Joomla.
You have to check the checkbox for which module you want to install. After installation, you will see a successful message.
Now You have to Go to Module Manager=>New Button
In the module type list click module: mod_latestposts
Like with other modules, you have to enter module title, select module position, menu assignment, etc.
In our module, we have to set the article(post) category and the number of posts on the front end.
And click on Save & Close button.
Now, this is the final output of the module. On the right side, you can see posts.
You may like to read these also: What is the Joomla Control Panel?