Shopping Basket

 x 
Cart empty


Popular Items

Find Us Elsewhere

Variable Form Field Documentation

The variable field form field can be used to generate self-replicating fields of most of the Joomla standard field types, including text, textarea, list and other list based types, such as category or usergroup. The user will see the standard input field, plus buttons which allow the user to add or delete input fields, also to re-order them, also to re-order them up and down.

We have included an example content plugin to show how it is used in both Joomla 1.5 and 1.6+. The plugin doesn't do anything, it just displays some examples of the form fields in the plugin parameters.

Usage - Joomla 1.5

To use the variable field in your Joomla 1.5 extension you need to copy the file elements/variablefield.php to an elements folder in your extension. Then in your extension manifest xml file you will need to add an 'addpath' attribute to the <params> tag, eg:-

<params addpath="/plugins/content/elements">

Then you create a form element in the normal way, giving it the type 'variablefield'. You set the type of field displayed by using the 'basetype' attribute, this can be any of the standard Joomla formfield types. If base type of the form field requires additional attributes (eg the rows and cols attributes of the textarea) then you set these as you would for a standard form field. There are two additional optional attributes, length (which sets the initial length of the variable field array and defaults to 1) and maxLength (which sets the maximum length of the array, and defaults to 0, which corresponds to no limit).

<param name="field2" type="variablefield"
description="Example variable textarea field"
label="Example variable textarea field"
basetype="textarea" length="3" maxLength="20" rows="3" cols="40"
/>

Usage Joomla 1.6/1.7/2.5

To use the variable field in your Joomla 1.6+ extension you need to copy the file fields/variablefields.php to a fields folder in your extension. Then in your extension manifest xml file you will need to add an 'addfieldpath' attribute to the <fields> tag, eg:-

<fields name="params" addfieldpath="/plugins/content/example_variablefield/fields">


Then you create a form field in the normal way, simply giving it the type 'variablefield'. You set the type of field displayed by using the 'basetype' attribute, this can be any of the standard Joomla formfield types. If base type of the form field requires additional attributes (eg the rows and cols attributes of the textarea) then you set these as you would for a standard form field. There are two additional optional attributes, length (which sets the initial length of the variable field array and defaults to 1) and maxLength (which sets the maximum length of the array, and defaults to 0, which corresponds to no limit).

<field name="field2" type="variablefield"
description="Example variable textarea field"
label="Example variable textarea field"
basetype="textarea" length="3" maxLength="20" rows="3" cols="40"
/>


Additonal Requirements

You will need to remember to load the elements and fields folders when your plugin is installed:-

<files>
<filename plugin="example_variablefield">example_variablefield.php</filename>
<filename>index.html</filename>
<folder>language</folder>
<folder>elements</folder>
<folder>fields</folder>
</files>


You will also need to ensure that the variablefield script is loaded. The variablefield script is assumed to be in the folder media/variablefield/js, because the media folder is the default folder for scripts in Joomla. To ensure that it is copied to there you must include the following in your manifest:-

<media destination="variablefield/js" folder="">
<filename>variablefield.js</filename>
</media>


Also remember to include the variablefield.js file in your installation package.

Using the Form Field Values in Your Extension

The form field values are returned as an array rather than a single value. However if there is only a single form field returned by the user this may result in a simple value rather than an array, which will cause an error if you try to treat it as an array, so it is best to cast this explicitly as an array:-

$value = (array) $params->get('field4',array());

Then you can access the values as elements of an array as normal, eg

<?php
foreach($value as $val)
{
  $article->text .=  ' value=  '.$val. ' ';
}
?>


Known Issues

Although in principal the variable field should work with any field type, there are problems with some types. The radio and checkbox types do not work, this is due to difficulties with the way radio groups and checkboxes are defined in html. Also the media and calendar types do not work, the problem is with the scripts that these field types use, they break when used in this context. It might be possible to rewrite the scripts to make these field types work.