Create a Custom Joomla 2.5 Module - Part 3
Table of Contents
Introduction
In this tutorial, we will guide you through the process of creating a custom Joomla 2.5 module that lists all featured articles on your Joomla website. This is an intermediate-level tutorial that builds on previous parts, enabling you to enhance your Joomla site with custom functionality. By the end, you'll have a working module that can be easily integrated and customized.
Step 1: Set Up Your Module Structure
To begin creating your custom module, you'll need to establish a proper file structure.
-
Create a New Folder:
- Navigate to the
modules
directory in your Joomla installation. - Create a new folder named
mod_featuredarticles
.
- Navigate to the
-
Create Required Files:
- Inside the
mod_featuredarticles
folder, create the following files:mod_featuredarticles.php
helper.php
index.html
(to prevent directory listing)mod_featuredarticles.xml
(for module configuration)
- Inside the
Step 2: Write the Module Code
Now, let's add the necessary code to our module files.
-
Open
mod_featuredarticles.php
:- This is the main entry point for your module. Add the following code:
defined('_JEXEC') or die; // Include the helper file require_once __DIR__ . '/helper.php'; // Get the featured articles $articles = ModFeaturedArticlesHelper::getFeaturedArticles(); // Load the template require JModuleHelper::getLayoutPath('mod_featuredarticles');
-
Create the Helper Functionality:
- Open
helper.php
and add the following code to retrieve featured articles:
defined('_JEXEC') or die; class ModFeaturedArticlesHelper { public static function getFeaturedArticles() { $db = JFactory::getDbo(); $query = $db->getQuery(true); $query->select('*') ->from('#__content') ->where('state = 1') ->where('featured = 1'); $db->setQuery($query); return $db->loadObjectList(); } }
- Open
-
Create the XML Manifest:
- In
mod_featuredarticles.xml
, define your module's metadata and parameters:
<extension type="module" version="2.5" client="site" method="upgrade"> <name>mod_featuredarticles</name> <author>Your Name</author> <description>Module to display featured articles</description> <files> <filename module="mod_featuredarticles">mod_featuredarticles.php</filename> <filename>helper.php</filename> <filename>index.html</filename> </files> </extension>
- In
Step 3: Create the Module Layout
To display the featured articles in your module, create a layout file.
-
Create a Layout File:
- In the
mod_featuredarticles
folder, create a new folder namedtmpl
. - Inside
tmpl
, create a file nameddefault.php
.
- In the
-
Add Layout Code:
- Open
default.php
and add the following code to loop through and display articles:
defined('_JEXEC') or die; if (!empty($articles)) { echo '<ul>'; foreach ($articles as $article) { echo '<li>' . htmlspecialchars($article->title) . '</li>'; } echo '</ul>'; } else { echo '<p>No featured articles found.</p>'; }
- Open
Step 4: Install and Test Your Module
Now that your module is created, it's time to install and test it.
-
Package Your Module:
- Zip the
mod_featuredarticles
folder.
- Zip the
-
Install the Module:
- Go to the Joomla Administrator panel.
- Navigate to Extensions > Extension Manager > Install.
- Upload the zipped module file.
-
Configure the Module:
- After installation, go to Extensions > Module Manager.
- Find the
mod_featuredarticles
module and set it to published. - Assign it to a position on your site.
-
Test the Module:
- Visit your Joomla site to see if the featured articles are displayed correctly.
Conclusion
You've successfully created and installed a custom Joomla 2.5 module to display featured articles. You can further customize the module by modifying the layout or adding additional features. Consider exploring Joomla's module parameters for more customization options. Happy coding!