This section covers the elements of a typical module, customization of a module, and module creation.. Understanding the elements of a typical module The system’s modules are divided int
Trang 1Given the wide number of variables related to component installers, and the fact that configuration
is very dependent upon the component itself, you should refer to the Installer API when it comes time to create a Component Installation Package The Installer API is located at: http://docs joomla.org/Using_the_installer_API_to_support_package_installation
Working with Modules
Modules are the most commonly modified pieces of the Joomla! core Modules are easy to custom-ize and are even relatively easy to create from scratch This section covers the elements of a typical module, customization of a module, and module creation
Understanding the elements of a typical module
The system’s modules are divided into two categories: Site modules and Administrator modules The front-end Site modules are located in the directory /modules The back-end Administrator modules are located in the directory /administrator/modules
The easiest way to see all the elements of a typical module is to look at an example The default system’s Login module is typical of Site modules throughout the system The Login module sup-plies a Login form for front-end users, along with links to the Password Reset and Username Reminder functions If front-end user registration is enabled in the Global Configuration Manager, then the module also includes a user registration link labeled Create an account
The Login module is a Site module and is located at modules/mod_login The contents of the directory are:
l helper.php
l index.html
l mod_login.php
l mod_login.xml
l /tmpl
l default.php
l index.html
The mod_modulename.php file
The mod_modulename.php is the principle functional file for the module This file is the first thing called by the system when the module is needed It contains the initialization routines and includes the helper.php file It will also call the template that will display the module output
Trang 2By way of example, here is the mod_login.php file:
// no direct access
defined(‘_JEXEC’) or die(‘Restricted access’);
// Include the syndicate functions only once
require_once (dirname( FILE ).DS.’helper.php’);
$params->def(‘greeting’, 1);
$type = modLoginHelper::getType();
$return = modLoginHelper::getReturnURL($params, $type);
$user =& JFactory::getUser();
require(JModuleHelper::getLayoutPath(‘mod_login’));
The code first prevents direct access to the file, a simple and common security precaution used throughout the Joomla! system Next the code includes the functions specified in the helper php file
The helper.php file
The file helper.php contains one or more classes that are used to retrieve the data that is dis-played by the module
The contents of the Login Form module’s helper.php are typical:
defined(‘_JEXEC’) or die(‘Restricted access’);
class modLoginHelper
{
function getReturnURL($params, $type)
{
if($itemid = $params->get($type))
{
$menu =& JSite::getMenu();
$item = $menu->getItem($itemid);
$url = JRoute::_($item->link.’&Itemid=’.$itemid, false);
}
else
{
// stay on the same page
$uri = JFactory::getURI();
$url = $uri->toString(array(‘path’, ‘query’, ‘fragment’));
}
return base64_encode($url);
}
function getType()
{
$user = & JFactory::getUser();
return (!$user->get(‘guest’)) ? ‘logout’ : ‘login’;
}
}
Trang 3The file defines the helper class modLoginHelper, which includes two functions to help set the return URL and determine the user’s status for the purpose of displaying either the logout or the login button
The XML file
The mod_modulename.xml file is also a required module file The XML file helps the Joomla! Installer determine what it needs to copy to install the module properly, and it also tells the Module Manager about the module, including not only basic identification information but also which parameters, if any, are available for module configuration
The mod_login.xml file follows:
<?xml version=”1.0” encoding=”utf-8”?>
<install type=”module” version=”1.5.0”>
<name>Login</name>
<author>Joomla! Project</author>
<creationDate>July 2006</creationDate>
<copyright>Copyright (C) 2005 - 2008 Open Source Matters All rights reserved.</copyright>
<license>http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
</license>
<authorEmail>admin@joomla.org</authorEmail>
<authorUrl>www.joomla.org</authorUrl>
<version>1.5.0</version>
<description>DESCLOGINFORM</description>
<files>
<filename module=”mod_login”>mod_login.php</filename>
</files>
<params>
<param name=”cache” type=”list” default=”1” label=”Caching” description=”Select whether to cache the content of this module”>
<option value=”0”>Never</option>
</param>
<param name=”@spacer” type=”spacer” default=”” label=”” description=”” />
<param name=”moduleclass_sfx” type=”text” default=””
label=”Module Class Suffix” description=”PARAMMODULECLASSSUFFIX” />
<param name=”pretext” type=”textarea” cols=”30” rows=”5” default=”” label=”Pre-text” description=”PARAMPRETEXT” />
<param name=”posttext” type=”textarea” cols=”30” rows=”5”
label=”Post-text” description=”PARAMPOSTTEXT” />
<param name=”login” type=”menuitem” default=””
disable=”separator” label=”Login Redirection URL” description=”PARAM LOGINREDIRECTURL” />
<param name=”logout” type=”menuitem” default=”” disable=”separator” label=”Logout Redirection URL” description=”PARAMLOGOUTREDIRECT URL” />
Trang 4<param name=”greeting” type=”radio” default=”1”
label=”Greeting” description=”Show/Hide the simple greeting text”>
<option value=”0”>No</option>
<option value=”1”>Yes</option>
</param>
<param name=”name” type=”list” default=”0” label=”Name/ Username”>
<option value=”0”>Username</option>
<option value=”1”>Name</option>
</param>
<param name=”usesecure” type=”radio” default=”0”
label=”Encrypt Login Form” description=”Submit encrypted login data (requires SSL)”>
<option value=”0”>No</option>
<option value=”1”>Yes</option>
</param>
</params>
</install>
The opening lines set out the type of file “module” and the associated Joomla! version number
“1.5.0” Immediately following is a series of tags that indicate:
l Module’s name
l Author’s name
l Creation date
l Copyright
l Module’s license agreement terms
l Author’s e-mail
l Author’s web site URL
l Module’s version number
l A short description
The remainder of the code defines the parameters that appear to the site administrator inside the Module Manager
The Module template
The /tmpl directory contains the template for the module The template file takes the data that has been generated by the primary module file and displays it on the page The key file that defines the module output is default.php, listed below
<?php // no direct access
defined(‘_JEXEC’) or die(‘Restricted access’); ?>
<?php if($type == ‘logout’) : ?>
Trang 5<form action=”index.php” method=”post” name=”login” id=”form-login”>
<?php if ($params->get(‘greeting’)) : ?>
<div>
<?php if ($params->get(‘name’)) : {
echo JText::sprintf( ‘HINAME’, $user->get(‘name’) );
} else : {
echo JText::sprintf( ‘HINAME’, $user->get(‘username’) );
} endif; ?>
</div>
<?php endif; ?>
<div align=”center”>
<input type=”submit” name=”Submit” class=”button”
value=”<?php echo JText::_( ‘BUTTON_LOGOUT’); ?>” />
</div>
<input type=”hidden” name=”option” value=”com_user” />
<input type=”hidden” name=”task” value=”logout” />
<input type=”hidden” name=”return” value=”<?php echo $return; ?>” />
</form>
<?php else : ?>
<?php if(JPluginHelper::isEnabled(‘authentication’, ‘openid’)) :
$lang->load( ‘plg_authentication_openid’, JPATH_ADMINISTRATOR );
$langScript = ‘var JLanguage = {};’
‘ JLanguage.WHAT_IS_OPENID = \’’.JText::_( ‘WHAT_IS_OPENID’ ).’\’;’
‘JLanguage.LOGIN_WITH_OPENID = \’’.JText::_( ‘LOGIN_WITH_OPENID’ ).’\’;’
‘JLanguage.NORMAL_LOGIN = \’’.JText::_( ‘NORMAL_LOGIN’ ).’\’;’
‘ var modlogin = 1;’;
$document = &JFactory::getDocument();
$document->addScriptDeclaration( $langScript );
JHTML::_(‘script’, ‘openid.js’);
endif; ?>
<form action=”<?php echo JRoute::_( ‘index.php’, true,
$params->get(‘usesecure’)); ?>” method=”post” name=”login” id=”form-login” >
<?php echo $params->get(‘pretext’); ?>
<fieldset class=”input”>
<p id=”form-login-username”>
<label for=”modlgn_username”><?php echo JText::_
(‘Username’) ?></label><br />
<input id=”modlgn_username” type=”text” name=”username” class=”inputbox” alt=”username” size=”18” />
</p>
<p id=”form-login-password”>
<label for=”modlgn_passwd”><?php echo JText::_(‘Password’)
?></label><br />
<input id=”modlgn_passwd” type=”password” name=”passwd” class=”inputbox” size=”18” alt=”password” />
</p>
Trang 6<?php if(JPluginHelper::isEnabled(‘system’, ‘remember’)) : ?> <p id=”form-login-remember”>
<label for=”modlgn_remember”>
<?php echo JText::_(‘Remember me’) ?>
</label>
<input id=”modlgn_remember” type=”checkbox” name=”remember” class=”inputbox” value=”yes” alt=”Remember Me” />
</p>
<?php endif; ?>
<input type=”submit” name=”Submit” class=”button” value=”<?php echo JText::_(‘LOGIN’) ?>” />
</fieldset>
<ul>
<li>
<a href=”<?php echo JRoute::_( ‘index.php?option=com_user&view=reset’ ); ?>”>
<?php echo JText::_(‘FORGOT_YOUR_PASSWORD’); ?></a>
</li>
<li>
<a href=”<?php echo JRoute::_( ‘index.php?option=com_
user&view=remind’ ); ?>”>
<?php echo JText::_(‘FORGOT_YOUR_USERNAME’); ?></a>
</li>
<?php $usersConfig = &JComponentHelper::getParams( ‘com_
($usersConfig->get(‘allowUserRegistration’)) : ?>
<li>
<a href=”<?php echo JRoute::_( ‘index.php?option=com_
user&view=register’ ); ?>”>
<?php echo JText::_(‘REGISTER’); ?></a>
</li>
<?php endif; ?>
</ul>
<?php echo $params->get(‘posttext’); ?>
<input type=”hidden” name=”option” value=”com_user” />
<input type=”hidden” name=”task” value=”login” />
<input type=”hidden” name=”return” value=”<?php echo $return; ?>”/>
<?php echo JHTML::_( ‘form.token’ ); ?>
</form>
<?php endif; ?>
This file plays the key role in the display of the module output The majority of the code in this file
is HTML used to create the various form fields needed by this module The PHP supplies the logic and the variables that relate to the language strings and the URL paths
Trang 7Two forms are on this page The first form provides the Logout button that appears when a user
is logged in to the system The second form is the Login Form that is displayed when a user is not logged in to the system The file uses PHP to establish an If/Else relationship between the two forms; showing the first form if a user is logged in If the user is not logged in, the second form is displayed
Note
The root module directory and the /tmpl directory both contain an index.html file This file serves no pur-pose other than to block persons from accessing the directory index by typing into their browsers the URL of the directory If a user enters the directory path as an URL, they will be served the index.html file, which dis-plays only a blank page You will see these files used throughout the directories of your Joomla! installation.
Overriding module output
The Joomla! system allows you override the output of a module by creating a new view template and inserting it into your active template directory This approach to overriding module output allows you to leave the core files untouched; all your changes are made to a new file that is segre-gated from the core module files This system has the added advantage of making it easy to keep
up with your modifications; no matter how many modules you override, all the changed files are all kept in one place
By way of an example, I am going to make a very basic modification to the output of the default Joomla! 1.5.x Login module To override the default template controlling the output of the Login module, follow these steps:
1 Copy the file /modules/mod_login/tmpl/default.php.
2 Open your active template directory and place the copied file inside a sub-directory
named /html/mod_login For example, if you are using the default rhuk_milkyway
tem-plate, you will create this: /templates/rhuk_milkyway/html/mod_login/ default.php
3 Make your changes to the new file.
4 Save your changes The new template will now override the original template and be
displayed on the site
To show this process in more detail, here are the code changes made to the Login module’s tem-plate file
As noted in the discussion of the module in Chapter 17, the Login module includes links to the Password Reset and Username Reminder functions, as shown in Figure 21.3 Those links are hard coded and cannot be disabled through the use of the Module parameters of the Global
Configuration options In this example, I eliminate those from the module
Trang 8FIGURE 21.3
The default display of the Login Form module Note the password and username links below the form
If you open the new template file created in the preceding steps, /templates/rhuk_
milkyway/html/mod_login/default.php, you see the following lines at the bottom of the second form:
<ul>
<li>
<a href=”<?php echo JRoute::_( ‘index.php?option=com_user&view=reset’ ); ?>”>
<?php echo JText::_(‘FORGOT_YOUR_PASSWORD’); ?></a>
</li>
<li>
<a href=”<?php echo JRoute::_( ‘index.php?option=com_
user&view=remind’ ); ?>”>
<?php echo JText::_(‘FORGOT_YOUR_USERNAME’); ?></a>
</li>
<?php $usersConfig = &JComponentHelper::getParams( ‘com_
($usersConfig->get(‘allowUserRegistration’)) : ?>
<li>
Trang 9<a href=”<?php echo JRoute::_( ‘index.php?option=com_
user&view=register’ ); ?>”>
<?php echo JText::_(‘REGISTER’); ?></a>
</li>
<?php endif; ?>
</ul>
The code sets out an unordered list containing three list items The first item generates a link to the Password Reset function The second item generates the link to the Username Reminder function The final item generates the User Registration link For this example, I am going to eliminate the first two items
Here’s how the same lines of code will look after modification:
<ul>
<?php $usersConfig = &JComponentHelper::getParams
($usersConfig->get(‘allowUserRegistration’)) : ?>
<li>
<a href=”<?php echo JRoute::_( ‘index.php?option=com_
user&view=register’ ); ?>”>
<?php echo JText::_(‘REGISTER’); ?></a>
</li>
<?php endif; ?>
</ul>
Note that I simply deleted the two list items embedding the links I wanted to eliminate I have not touched the UL tag that wrapped them, nor have I cut out any of the code related to the User Registration function This modification will remove the two links, as shown in Figure 21.4 Aside from the cosmetic change, the User Registration function works normally
Cross-Reference
See also Chapter 20 for a discussion of how to impact module output appearance by working with the module chrome.
Save this file and the change is done Your new override will take precedence over the original template — and, importantly, you have accomplished this without making any changes to the original module
Tip
If you don’t want to cut the code out of your template file, then simply wrap it in comment tags to remove it from processing Note also that the best practice is to use PHP comment tags, not HTML comment tags.
Trang 10FIGURE 21.4
The modified Login Form module
Creating a new module
A detailed tutorial on all the aspects of module creation is outside the scope of this chapter, but you should understand the basics of creating a new module If you want to explore this topic in more detail, you can find a number of excellent resources on the topic in the official Joomla! Developer documentation
Note
To help get you started, the Joomla documentation site includes a basic module tutorial called Creating a Hello World Module for Joomla 1.5 at
http://docs.joomla.org/Tutorial:Creating_a_Hello_World_Module_for_
Joomla_1.5
Tip
Don’t forget that the Joomla! core includes the module type Custom HTML That is a blank module that allows you to insert whatever content you wish If you need only to create a module to hold content, there may be no reason for you to go through the process of coding a new module; instead, try creating a new module by using