Tutorial: howto write a Joomla module

This tutorial shows how to write a Joomla module that performs a database query and outputs the result.

This module will display in a little box five articles, belonging to a given category and section, based upon the “Finish Publishing” date.

Let’s suppose you have a section named “Events” and a category “Sport Events”. We want to show in the module the next five events. How do we know which are the next events? We use a Joomla administrator field named “Finish Publishing”. Here we set a date that will be the date when the event will happen.

Finish Publishing

We use as a starting point the most simply module that is delivered within the Joomla standard installation, mod_related_items.
Open the folder joomla/modules/ and copy mod_related_items.php and mod_related_items.xml. Rename them as mod_next_events.php and mod_next_events.xml and open them in a text editor.

The .xml file is used to set some parameters that identify the module. Here’s an example of how to edit this file:

<?xml version="1.0" encoding="iso-8859-1"?>
<mosinstall type="module" version="1.0.0">
<name>Next Events</name>
<author>Copes Flavio</author>
<creationDate>May 2007</creationDate>
<copyright>(C) 2007 Open Source Matters. All rights reserved.</copyright>
<license>http://www.gnu.org/copyleft/gpl.html GNU/GPL</license>
<authorEmail>copesc@gmail.com</authorEmail>
<authorUrl>www.copesflavio.com</authorUrl>
<version>1.0.0</version>
<description>Shows the 5 next sport events.</description>
<files>
<filename module="mod_next_events"> mod_next_events.php</filename>
</files>
<params>
<param name="cache" type="radio" default="0" label="Enable Cache" description="Select whether to cache the content of this module">
<option value="0">No</option>
<option value="1">Yes</option>
</param>
<param name="moduleclass_sfx" type="text" default="" label="Module Class Suffix" description="A suffix to be applied to the css class of the module (table.moduletable), this allows individual module styling" />
</params>
</mosinstall>

Now open the file mod_next_events.php.

Go to the line if ($option == 'com_content' && $task == 'view' && $id)
Delete this if () and write

$query = "SELECT a.id, a.title, a.sectionid, a.catid, cc.access AS cat_access, s.access AS sec_access, cc.published AS cat_state, s.published AS sec_state"
. "\n FROM #__content AS a"
. "\n LEFT JOIN #__content_frontpage AS f ON f.content_id = a.id"
. "\n LEFT JOIN #__categories AS cc ON cc.id = a.catid"
. "\n LEFT JOIN #__sections AS s ON s.id = a.sectionid"
. "\n WHERE a.state = 1"
. "\n AND a.access <= " . (int) $my->gid
. "\n AND sectionid=5 AND catid=15"
. "\n AND publish_down > NOW() AND publish_down <> '0000-00-00 00:00:00'"
. "\n GROUP BY publish_down desc limit 5;"
;
$database->setQuery( $query );
$temp = $database->loadObjectList();
$prossimi = array();
if (count($temp)) {
foreach ($temp as $row ) {
if (($row->cat_state == 1 || $row->cat_state == '') && ($row->sec_state == 1 || $row->sec_state == '') && ($row->cat_access <= $my->gid || $row->cat_access == '') && ($row->sec_access <= $my->gid || $row->sec_access == '')) {
$prossimi[] = $row;
}
}
}
unset($temp);
if ( count( $prossimi ) ) {
?>
<ul>
<?php
foreach ($prossimi as $item) {
if ($option=”com_content” && $task=”view”) {
$Itemid = $mainframe->getItemid($item->id);
}
$href = sefRelToAbs( “index.php?option=com_content&task=view&id=$item->id&Itemid=$Itemid” );
?>
<li>
<a href=”<?php echo $href; ?>”>
<?php echo $item->title; ?></a>
</li>
<?php
}
?>
</ul>
<?php
}

What does it mean? We execute a SQL query that extracts from the database the next 5 sport events, that have a section id 5 and category id 15, and we show them in a unordered list.

The result will be something as

Events example

Here you can see only two events because the module only found two entries in the db.

Obviously this module has to be installed and assigned to a page, like every module.

Share and Enjoy:
  • Digg
  • del.icio.us
  • StumbleUpon
Tags: , , ,

4 Responses to “Tutorial: howto write a Joomla module”

  1. Mehera Says:

    I am a young girl of 22 years old. I am a joomla beginner. I want learn how to write a joomla module.

    I hope your kindfull attention.

    Best rgds
    Mehera

  2. Copes Flavio Says:

    Hi, this tutorial is outdated since it talks about Joomla! 1.0.x and not the new branch 1.5.x.

    Since you’re new to Joomla, I suggest you to read a book that talks about joomla and introduces you to extension development such as this http://www.packtpub.com/Joomla

    Cheers Flavio

  3. Cheeky Monkey Says:

    Hi

    I can see that there is an option to enable cache

    Can you describe how the module caching is managed?

  4. graphicsresources.net Says:

    How to write a Joomla module…

    This tutorial shows how to write a Joomla module that performs a database query and outputs the result….

Leave a Reply

Name (obbligatorio)

Mail (will not be published) (obbligatoria)

Website