Creating Custom Parameters in Joomla 1.6 Print

Creating Custom Parameters in Joomla 1.6

In the course of updating our extensions to Joomla 1.6 we have managed to figure out some useful information about the differences between Joomla 1.5 and 1.6. There have been some big changes in the format of the installation package, which this article explores.

Format of the Installation Manifest

There have been some changes to the format of the installation manifest xml file. The root <install> tag has been replaced with an <extension> tag, while the <params> and <param> tags have been replaced with the <fields> and <field> tag respectively. The <fields> tags are now placed in a <config> tag container. The rest of the layout remains much the same. So a typical layout for a module in Joomla 1.6 would be:-

***

<?xml version="1.0" encoding="utf-8"?>
<extension type="module" version="1.6.0" method="upgrade" client="site">
<name>My Module</name>
<author>Jo Bloggs</author>
<creationDate>July 2010</creationDate>
<copyright>Inspiration Web Design</copyright>
<license>GPL</license>
<authorEmail> This e-mail address is being protected from spambots. You need JavaScript enabled to view it </authorEmail>
<authorUrl>www.mysite.com</authorUrl>
<version>1.0.0</version>
<description>Mails a link to the current page</description>
<files>
<filename module="my_module">my_module.php</filename>
<filename>helper.php</filename>
<filename>index.html</filename>
<filename>tmpl/default.php</filename>
<filename>tmpl/index.html</filename>
<folder>assets</folder>
</files>
<config>
<fields name="params">
<fieldset name="basic" label="Module Parameters" description="Configure Module">
<field name="fromEmail" type="text" default="" label="from email address" />
<field name="bcc" type="text" default="" label="bcc" description="DESCBCC" />
<field name="filterEmails" type="textarea" cols="20" rows="3" label="invalid emails" />
<field name="moduleclass_sfx" type="text" default="" label="Module Class Suffix" />
<field name="use_slider" type="list" default="0" label="Use mootools slider" >
<option value="1">Yes</option>
<option value="0">No</option>
</field>
<field name="use_cookie" type="list" default="1" label="Remember slide state" >
<option value="1">Yes</option>
<option value="0">No</option>
</field>
<field name="use_detection" type="list" default="0" label="Use Extended URL detection">
<option value="1">Yes</option>
<option value="0">No</option>
</field>
</fieldset>
</fields>
</config>
<languages>
<language tag="en-GB">en-GB.my_module.ini</language>
<language tag="en-US">en-US.my_module.ini</language>
</languages>
</extension>


***

 

Adding Custom Parameters

So how do you go about adding custom parameters? If you are familiar with doing this with Joomla 1.5 you will remember that this involved extending the JElement Class, and overriding the fetchElement() method. This method was used to generate the html for the required field.

In Joomla 1.6 the process is quite similar, in this case we need to extend the JFormField Class. This Joomla class includes a getInput() method, which we can override to generate the html for the form field. The main difference from Joomla 1.5 is in how we access the name, value and other attributes of the form field. In Joomla 1.5 these were passed as parameters to the fetchElement() method. In Joomla 1.6 these are pre-populated as properties of the JFormField object that we are working with. So, for example to access the name of the form field we simply refer to it by $this->name. The class include a variable $type which must be set to the name of the field type.

In case this is not all crystal clear, the best way to illustrate this is with a example. The example below generates a list of currently published Joomla articles arranged by category.

<?php
// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );


jimport('joomla.html.html');
jimport('joomla.form.formfield');//import the necessary class definition for formfield


/**
* Supports an HTML select list of articles
* @since 1.6
*/
class JFormFieldArticles extends JFormField
{
/**
* The form field type.
*
* @var string
* @since 1.6
*/
protected $type = 'Articles'; //the form field type

/**
* Method to get content articles
*
* @return array The field option objects.
* @since 1.6
*/
protected function getInput()
{
// Initialize variables.
$session = JFactory::getSession();
$options = array();

$attr = '';

// Initialize some field attributes.
$attr .= $this->element['class'] ? ' class="'.(string) $this->element['class'].'"' : '';

// To avoid user's confusion, readonly="true" should imply disabled="true".
if ( (string) $this->element['readonly'] == 'true' || (string) $this->element['disabled'] == 'true') {
$attr .= ' disabled="disabled"';
}

$attr .= $this->element['size'] ? ' size="'.(int) $this->element['size'].'"' : '';
$attr .= $this->multiple ? ' multiple="multiple"' : '';

// Initialize JavaScript field attributes.
$attr .= $this->element['onchange'] ? ' onchange="'.(string) $this->element['onchange'].'"' : '';


//now get to the business of finding the articles

$db = &JFactory::getDBO();
$query = 'SELECT * FROM #__categories WHERE published=1 ORDER BY parent_id';
$db->setQuery( $query );
$categories = $db->loadObjectList();

$articles=array();

// set up first element of the array as all articles
$articles[0]->id = '';
$articles[0]->title = JText::_("ALLARTICLES");

//loop through categories
foreach ($categories as $category) {
$optgroup = JHTML::_('select.optgroup',$category->title,'id','title');
$query = 'SELECT id,title FROM #__content WHERE catid='.$category->id;
$db->setQuery( $query );
$results = $db->loadObjectList();
if(count($results)>0)
{
array_push($articles,$optgroup);
foreach ($results as $result) {
array_push($articles,$result);
}
}
}

// Output

return JHTML::_('select.genericlist', $articles, $this->name, trim($attr), 'id', 'title', $this->value );

}
}


Simples!

Note that there are other ways of tackling this. Among the predefined field types that Joomla 1.6 supplies is the 'list' type, which defines the JFormFieldList class, which in turn is an extension of JFormField. We could have chosen to extend The JFormFieldList instead. This class returns a generic select list field. It includes a method, getOptions(), which returns the options available. We could have chosen to over-ride this method to return the articles as a list of options

Adding the Custom Form Field to The Installation Package

The custom form field is added to your installation package using an addfieldpath attribute added to the < fields> tag. In the example below we assume that the articles field definition is placed in a file called articles.php which is placed in a folder /modules/mod_mymodule/fields (note that this is relative to the root folder of your site).

 <fields name="params" addfieldpath="/modules/mod_mymodule/fields">


Backwards Compatibility With Joomla 1.5

In fact we have found that the Joomla 1.6 installer will understand the old <install> tag, so it is possible to create an installation package that will install in Joomla 1.6 and Joomla 1.6. To do this, use the <install> tag:-

<install type="module" version="1.5.0" method="upgrade" client="site">

The parameters for Joomla 1.6 can be added in a <config> tag, while the old <params> tag can be used for the Joomla 1.5 parameters. The installers will simply ignore the tags that they do not use.

If you want to add custom parameters for versions 1.5 and 1.6 we have found that the easiest way to do this is to have separate folders for the separate definitions of the custom fields, for example called 'elements' and 'fields' respectively.

 

 


Written on Monday, 02 August 2010 12:39 by Spiral Scripts

Viewed 2427 times so far.
Like this? Tweet it to your followers!

Latest articles from Spiral Scripts

Latest 'tweets' from Spiral Scripts

blog comments powered by Disqus