Create a Custom Joomla 2.5 Module - Part 3

3 min read 2 hours ago
Published on Sep 21, 2024 This response is partially generated with the help of AI. It may contain inaccuracies.

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.

  1. Create a New Folder:

    • Navigate to the modules directory in your Joomla installation.
    • Create a new folder named mod_featuredarticles.
  2. 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)

Step 2: Write the Module Code

Now, let's add the necessary code to our module files.

  1. 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');
    
  2. 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();
        }
    }
    
  3. 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>
    

Step 3: Create the Module Layout

To display the featured articles in your module, create a layout file.

  1. Create a Layout File:

    • In the mod_featuredarticles folder, create a new folder named tmpl.
    • Inside tmpl, create a file named default.php.
  2. 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>';
    }
    

Step 4: Install and Test Your Module

Now that your module is created, it's time to install and test it.

  1. Package Your Module:

    • Zip the mod_featuredarticles folder.
  2. Install the Module:

    • Go to the Joomla Administrator panel.
    • Navigate to Extensions > Extension Manager > Install.
    • Upload the zipped module file.
  3. 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.
  4. 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!