3 min read

Introduction

Modules in Joomla can be used to fetch and display data almost anywhere on a page in a website.In this article, we will cover the following topics on module development.

  • Registering the module in the database
  • Getting and setting parameters
  • Centralizing data access and output using helper classes
  • Selecting display options using layouts
  • Displaying the latest reviews
  • Displaying a random review

We will assume that we have a table in our database called jos_modules with the following fields title, ordering, position, published, module, showtitle, and params. We will also assume that we have a website that reviews different restaurants. However, visitors have to go to the component to see the reviews. We would be developing a module so that we could pull the content directly from the reviews and display them.

Registering the Module in the Database

As with the component, we will have to register the module in the database so that it can be referenced in the back end and used effectively. Entering a record into the jos_modules table will take care of this. Open your database console and enter the following query:

INSERT INTO jos_modules (title, ordering,
    position, published, module, showtitle, params)
    VALUES ('Restaurant Reviews', 1, 'left', 1,
    'mod_reviews', 1, 'style=simplenitems=3nrandom=1');

If you’re using phpMyAdmin, enter the fields as in the following screen:

Module Development in Joomla

If you refresh the front end right after entering the record in jos_modules, you’ll notice that the module doesn’t appear, even though the published column is set to 1. To fix this, go to Extensions | Module Manager in the back end and click the Restaurants Reviews link. Under Menu Assignment, select All and click Save.

Module Development in Joomla

In the front end, the left-hand side of your front page should look similar to the following:

Module Development in Joomla

Creating and Configuring a Basic Module

Modules are both simple and flexible. You can create a module that simply outputs static text or one that queries remote databases for things like weather reports. Although you can create rather complex modules, they’re best suited for displaying data and simple forms. You will not typically use a module for complex record or session management; you can do this through a component or plug-in instead.

To create the module for our reviews, we will have to create a directory mod_reviews under /modules. We will also need to create the mod_reviews.php file inside mod_reviews. To start, we’ll create a basic module that displays links to the most recent reviews. In the mod_reviews.php file, add the following code:

<?php
defined('_JEXEC') or die('Restricted access');
$items = $params->get('items', 1);
$db =& JFactory::getDBO();
$query = "SELECT id, name FROM #__reviews WHERE
          published = '1' ORDER BY review_date DESC";
$db->setQuery( $query, 0, $items );
$rows = $db->loadObjectList();
foreach($rows as $row)
{
    echo '<a href="' . JRoute::_('index.php?option=com_reviews&id='
    . $row->id . '&task=view') . '">' . $row->name .
    '</a><br />';
}
?>

When you save the file and refresh the homepage, your module should look similar to the following:

Module Development in Joomla

When the module is loaded, the $params object is pulled into scope and can be used to get and set the parameters. When we added the row into jos_modules, the params column contained three values: one for items (set to 3), one for style (set to simple), and another for random (set to 1). We set $items to the parameter items using the get() member function, defaulting to 1 if no value exists.

If desired, you can use the member function set($name, $value) to override or add a parameter for your module.

After getting a database object reference, we write a query to select the id and name form jos_reviews and order reverse chronologically by the published date. We use the second and third parameters of setQuery() to generate a LIMIT clause that is automatically added to the query. This ensures that the correct syntax is used for the database type. Once the query is built, we load all the relevant database rows, go through them, and provide a link to each review.


LEAVE A REPLY

Please enter your comment!
Please enter your name here