Actions

User

Rvsjoen/tutorial/Developing a Module/Part 03

From Joomla! Documentation

< User:Rvsjoen‎ | tutorial/Developing a Module

Contents

Using parameters

So you want your module to be configurable using parameters, eh? In this part we will provide you with a basic example on how to add a parameter to the XML manifest so that it can be configured in the module manager. In addition we will show you how to access that parameter in the module code.

With your favorite editor, edit the following file

tmpl/default.php

<?php 
 
/**
 * @package     Joomla.Tutorials
 * @subpackage  Module
 * @copyright   Copyright (C) 2005 - 2010 Open Source Matters, Inc. All rights reserved.
 * @license     License GNU General Public License version 2 or later; see LICENSE.txt
 */
 
// No direct access to this file
defined('_JEXEC') or die;
?>
 
<p><?php echo $params->get('greeting', 'I am a module'); ?></p>

As you can see by the highlighted line, we expect a parameter named greeting to be available in the parameters of the module. Should the parameter not be available we will use the default value "I am a module" which is passed as the second argument.

With your favorite editor, edit the following file

mod_helloworld.xml

<?xml version="1.0" encoding="utf-8"?>
<extension type="module" version="2.5.0" method="upgrade">
 
        <name>Hello World!</name>
        <!-- The following elements are optional and free of formatting constraints -->
        <creationDate>Once upon a time</creationDate>
        <author>John Doe</author>
        <authorEmail>john.doe@example.org</authorEmail>
        <authorUrl>http://www.example.org</authorUrl>
        <copyright>Copyright Info</copyright>
        <license>License Info</license>
        <!--  The version string is stored in the extension table -->
        <version>0.0.3</version>
        <!-- The description is optional and defaults to the name -->
        <description>Description of the Hello World module ...</description>
 
        <!-- Note the folder attribute: This attribute describes what to copy
                into the module folder -->
        <files>
                <filename module="mod_helloworld">mod_helloworld.php</filename>
                <filename>mod_helloworld.xml</filename>
                <folder>tmpl</folder>
        </files>
 
        <config>
                <fields name="params">
                        <fieldset name="basic">
                                <field
                                        name="greeting"
                                        type="text"
                                        default="I am a module"
                                        label="Greeting"
                                        description="The greeting to display"
                                />
                        </fieldset>
                </fields>
        </config>
 
</extension>

The most important part of this modification is the introduction of the <config> element. As you can see we use it to add a single field named greeting to the module manifest. The field is of type text and has a label and description. After adding this to the XML file the parameter should display automatically in the options when editing the module in the module manager.

File listing

Testing your module

For details on how to install the module into your Joomla! site, refer to the information provided in Part 01.

Download this part

Download example package

Articles in this series

This tutorial is supported by the following versions of Joomla!

Joomla 2.5