Sunday, 22 November 2015 20:17

Building Joomla Modules - Part 3: Core module files

Written by
Rate this item
(0 votes)

In this article I am going to demonstrate "core" module files. There are three important files. First two are located within module folder ( mod_tksdfractionslider.php and helper.php ) another is inside /tmp folder and its name is default.php ( it is our view ). Lets start from main module entry point which is in our case mod_tksdfractionslider.php.

 * @package TKSD Fraction slider for Joomla 3+
 * @version 1.0.0
 * @author Tomasz Kisielewski
 * @copyright (C) 2014- tkstudiodesign
 * @website 
 * @license GNU/GPLv3

defined('_JEXEC') or die; //add stylesheet $doc =& JFactory::getDocument(); require_once(dirname(__FILE__).'/helper.php'); $moduleclass_sfx = htmlspecialchars($params->get('moduleclass_sfx')); $listofimages = mod_tksdfractionsliderHelper::getImages($params); mod_tksdfractionsliderHelper::load_jquery($params); $doc->addStyleSheet(JURI::base(true) . '/modules/mod_tksdfractionslider/assets/css/fractionslider.css', 'text/css' ); $doc->addStyleSheet(JURI::base(true) . '/modules/mod_tksdfractionslider/assets/css/style.css', 'text/css' ); $doc->addScript(JURI::base(true) . '/modules/mod_tksdfractionslider/assets/js/jquery.fractionslider.js'); require(JModuleHelper::getLayoutPath('mod_tksdfractionslider'));

We have typical php tag at the beginning of the file, followed by our header with all information about author, copyrights, licensing ect. Next we have very important line:

defined('_JEXEC') or die;

This checks if it is invoked within Joomla! environment not from outside, by checking if _JEXEC is defined. If not it stops execution, well..dies. Then we get document:

$doc=& JFactory::getDocument();

Next step is get helper.php file with its methods


As you know Joomla allows us to add additional suffixes for our module. It helps generally with styling. This way you can add additional class, but remember to add leading space! We escape user input with php function htmlspecialchars().

$moduleclass_sfx = htmlspecialchars($params->get('moduleclass_sfx'));

In next step we create list of our images ( and captions ) We create variable $listofimages which is array populated by function from our helper file.

$listofimages = mod_resliderHelper::getImages($params);

Another function from our helper which basically loads jquery based on our xml configuration settings.


Last part is very important. We attache css styles and jquery files for plugin. These files are what we downloaded with our plugin. If you want to attach more files, do it here.

$doc->addStyleSheet(JURI::base(true) . '/modules/mod_tksdfractionslider/assets/css/fractionslider.css', 'text/css' );

$doc->addStyleSheet(JURI::base(true) . '/modules/mod_tksdfractionslider/assets/css/styleDEF.css', 'text/css' );

$doc->addScript(JURI::base(true) . '/modules/mod_tksdfractionslider/assets/js/jquery.fractionslider.js');

Last bit for calling default view for our module.




This is where real fun begins. In this file we placed all our logic. As usually I put at the beginning licensing, author information and our “magic” _JEXEC sentence.


abstract class mod_tksdfractionsliderHelper{
	public static function getImages(&$params){	
	$imgsAndCaps = array(); 
	$i = 1;
	while($i < 10):
		$step= $i;

See GitHub We create abstract class tksdfractionsliderHelper ( abstract means we cant create object of this class) and within class we declare two methods. First is

static function getImages(&$params)

For our image and captions which is called from mod_tksdfractionslider.php ( line:16)

$listofimages = mod_tksdfractionsliderHelper::getImages($params);

and another is:

static function load_jquery(&$params)

This method is responsible for jQuery plugin configuration options and like previous one is called from main module file


I think you got picture, helper.php is like "storage" file where we create all required stuff and use it somewhere elase. Ok lets talk about first method which is getImages static function. We pass params as arguments.

class mod_resliderHelper{


public function getImages(&$params){

$imgsAndCaps = array();

$i = 1;

while($i < 10):

if(($params->get('image'.$i))&& ($params->get('image'.$i.'cap'.$i) || $params- >get('image'.

$i.'cap'.($i+1)) || $params->get





Lets talk about first method getImages(&$params). It accepts $params as partameters. First we create array to store our configuration options and assign it to variable $imgsAndCaps.


$imgsAndCaps = array();


Because we have more than one image, to make processing easier we added number to names ( I have already mentioned about naming conventions in this module like image1cap1BackgroundColor what is background color for caption first etc). For doing so we need to have our “step” variable $i which is initialized to one.


$i = 1;


Secondly we are using while block to set up data for 10 images ( you may change it as you wish )


while($i < 10):


next we are doing some checking. I have decided that for this slider to work we need image


if(($params->get('image'.$i))&& .....


and at least one caption, doesn't matter which one.


… $params->get('image'.$i.'cap'.$i) || $params->get('image'.$i.'cap'.($i+1)) || $params->get

('image'.$i.'cap'.($i+2)))){ …..


$params->get('nameOfParameter') is how you get parameters from XML configuration file ( mod_tksdfractionslider.xml ). How it works is simple. If we take first image, $i is equal to 1, we have $params->get('image'.$i.'cap'.$i) equal to “image1cap1” in our XML file ( <field name="image1cap1"...), 'image'.$i.'cap'.($i+1) gives us <field name="image1cap2" type=.....

This “if” statement is where I enforced my rule of presence image and at least one caption. After statement returns true we age going to use variable $i, what means that will be incremented every loop ( All configuration options will be this way connected to one particular image (image+$i).


Now we are assigning values from mod_tksdfractionslider.xml to variables.

























That all variables related to single image. Using $i variable we don't have to repeat this step 10 times as we deal with ten images. We are still “within the loop”, now we place all our variables for this iteration into array, where all data related to image1 will be inside array $imgsAndCaps[1], related to image number to inside $imgsAndCaps[2].


























The last bits of this method are, incrementing our $i variable by one:


Close our while loop


and of course we need to return our $imgsAndCaps variable, which is an array.

return $imgsAndCaps;

We get all this bunch of images and options when we call this method in mod_tksdfractionslider.php

$listofimages = mod_resliderHelper::getImages($params);

The last method of this class is for inclusion javascript files, check version of installed Joomla and acts accordiongly.

public function load_jquery(&$params){


JLoader::import( 'joomla.version' );

$version = new JVersion();

if (version_compare( $version->RELEASE, '2.5', '<=')) {

$doc = &JFactory::getDocument();

$app = &JFactory::getApplication();





} else {









This file is a “view” for our module. It contains HTML, CSS classes, javascript mixed with our configuration variables. It is located in /tmp directory and starts with standard sentences package, licensing, visioning, author information and “magic” _JEXEC statement.




* @package TKSD Fraction slider for Joomla 3+

* @version 1.0.0

* @author Tomasz Kisielewski

* @copyright (C) 2014-2016- tkstudiodesign

* @website

* @license GNU/GPLv3



defined('_JEXEC') or die;




Before we start codding php is worth to look at pure html file, which is in our downloaded fractionslider package ( fractionslider/example/index.html )

You can see that there are setting which are easy to replace with php code and Joomla! parameters.

We use similar structure, but not that many images. I leave room for you to improve this module. After all if you get how it is done in Joomla, you will be able to work and experiment with any jquery slider trying to fit it into your needs.

<div class="slider">
  <div class="fraction-slider">
  <div class="fs_loader"></div>
  	foreach($listofimages as $key => $value){?>
        <div class="slide" data-in="fade">
       <?php if(isset($listofimages[$key]["caption1"])){?>
        <p class="teaser small" style="background-color:<?php echo $listofimages[$key]["caption1BackgroundColor"];?>; width:20% !important;" data-position="<?php echo $listofimages[$key]["captionPositionX1"];?> ,<?php echo $listofimages[$key]["captionPositionY1"];?>" data-in="<?php echo $listofimages[$key]["caption1DataIn1"]; ?>" data-out="<?php echo $listofimages[$key]["caption1DataOut1"]; ?>" data-step="1" data-ease-in="<?php echo $listofimages[$key]["caption1Animation"];?>" data-delay="500"><?php echo $listofimages[$key]["caption1"]; ?> </p><?php } ?>   
        <?php if(isset($listofimages[$key]["caption2"])){?>





Read 930 times