Class FieldTypeLink extends FieldTypeChar {
function default_Disp($field_value="") {
if (!$this->getRData('hidden')) {
$this->processed .= “”.$field_value.””;
}
}
}
We overwrite the method default_Disp to display a link for that value.
To load that class in your application create in the WebIDE->Web Page the following configuration file: **includes/FieldTypeLink.conf.inc.php**
With the following content:
To test the new field type create a table with the WebIDE with a text field call **weburl**.
Edit the registry XML source associated with your table and replace the **rfield** weburl with the following:
1
Web Page
10:30
FieldTypeLink
By extending the FieldTypeChar our Link field type inherits the rdata text line.
The only required rdata is the fieldtype with the name of the FieldType class : FieldTypeLink.
===== Phone Number example =====
As a second example we will create an input mask for a phone number. We want the phone number to be typed into 3 fields and display them in the form of : (310)-370-3365 but store them in the database as a string of 10chars : 3103703365.
==== Registry Field Object ====
Lets start by creating our new FieldType class. In the WebIDE in the class tab create a new class called: **FieldUsPhone.class.php**
getRData('hidden') && !$this->getRData('readonly')) {
if ($this->getRData('css_form_class')) {
$field_class = $this->getRData('css_form_class');
} else { $field_class = "adformfield"; }
$international_prefix = "";
$extention = "";
if ($this->getRData('international') || $this->getRData('extention')) {
list($field_value, $international_prefix, $extention) = explode("||", $field_value);
}
$e_format = new Event("FieldUsPhone->eventFormatUsPhone");
$e_format->setSecure(false);
$e_format->setHideKey(true);
$e_format->setLevel(900);
$fval .= $e_format->getFormEvent();
$fval .= "getFieldName()."\">";
if ($this->getRData('international')) {
$fval .= "\n+getFieldName()."]\"
value=\"".$international_prefix."\"
type=\"text\" size=\"2\"> -";
}
$fval .= "\n(getFieldName()."]\"
value=\"".substr($field_value, 0, 3)."\"
type=\"text\" size=\"3\">)";
$fval .= "\n-getFieldName()."]\"
value=\"".substr($field_value, 3, 3)."\"
type=\"text\" size=\"3\">";
$fval .= "\n-getFieldName()."]\"
value=\"".substr($field_value, 6, 4)."\"
type=\"text\" size=\"4\">";
if ($this->getRData('extention')) {
$fval .= "\n Ext:getFieldName()."]\"
value=\"".$extention."\"
type=\"text\" size=\"5\">";
}
$this->processed .= $fval;
}
}
function default_Disp($field_value="") {
if (!$this->getRData('hidden')) {
$international_prefix = "";
$extention = "";
if ($this->getRData('international') || $this->getRData('extention')) {
list($field_value, $international_prefix, $extention) = explode("||", $field_value);
}
if (!empty($international_prefix)) { $international_prefix = "+".$international_prefix." "; }
if (!empty($extention)) { $extention = " Ext:".$extention; }
if (strlen($field_value) > 0) {
$this->processed .= $international_prefix."(".substr($field_value, 0, 3).")-".substr($field_value, 3, 3)."-".substr($field_value, 6, 4).$extention;
}
}
}
}
?>
You can see in the script above the FieldUsPhone field type takes a single field and displays it in 3, 4 or 5 form fields.
Some custom options (RData) are set like **international** and **extention**.
$this->getRData('hidden'); can be written as: $this->hidden
fields;
$phonefieldname = $event_controler->phonefieldname;
$areaphone = $event_controler->areaphone;
$secondphone = $event_controler->secondphone;
$thirdphone = $event_controler->thirdphone;
$internationalprefix = $event_controler->internationalprefix;
$extention = $event_controler->extention;
foreach ($phonefieldname as $phonefield) {
if (!empty($internationalprefix[$phonefield]) || !empty($extention[$phonefield])) {
$extra = "||".$internationalprefix[$phonefield]."||".$extention[$phonefield];
} else {
$extra = "";
}
$dbfield = $areaphone[$phonefield].$secondphone[$phonefield].$thirdphone[$phonefield].$extra ;
$fields[$phonefield] = $dbfield;
}
$event_controler->fields = $fields;
}
// Continue the class
}
?>
We get the fields array from the event controller, parse all of the phonefieldname fields, concatenate the field values and store the phone field in the fields array.
Before finishing we update the fields array in the event controller parameters.
We uses arrays indexed by field names to support multiple fields of that type in one form.
The **$fields** array is indexed by field name and contains field values submitted from the form.
This array will be used further in the [[core:events|Event]] execution a mydb.updateRecord or mydb.addRecord Event Action may process the fields array and update the data in the database.
==== Auto Load configuration ====
To load the field type class in the application we need to create an auto include configuration file.
In the WebIDE -> Web Pages create the following file: **includes/FieldUsPhone.conf.inc.php**
==== Registry XML source ====
Now to attach this new field type add a **phone_number** text field to one of your table and then edit its registry.
The registry rfield for a us phone field will look like this :
Phone
1
1
FieldUsPhone
The international prefix and extention are optional, by default if their rdata are not in the registry they will not display.
===== Add to WebIDE =====
Instead of having to edit the Registry source file to add your custom field type, it is more convenient to directly add it in the WebIDE.
You will need to create 2 or 3 additional files. Improve your configuration file to register the field to WebIDE field type arrays.
==== Configuration File ====
In the configuration file: **includes/FieldUsPhone.conf.inc.php **
The **$cfg_fieldType** array is used by WebIDE to register the field.
Then we assign a variable with the same name to give it a human readable label.
==== Options Form ====
Next we need to provide some information for the options form. The simplest way is to create a Registry.
And if you need further customization you can create a custom form.
=== Registry ===
With the WebIDE create a new registry called **FieldUsPhone.reg.xml** for each **RData** used by our field type.
The **FieldUsPhone.reg.xml** source code will looks like:
International Prefix
Yes:No
1:0
1
strFBFieldTypeListBoxSmall
Label
:
strFBFieldTypeChar
Default
50:
strFBFieldTypeChar
Extention
Yes:No
1:0
1
strFBFieldTypeListBoxSmall
PageBuilderFieldListStyles
CSS Class style
yes
=== Custom Form ===
If you create custom field types you will want them to work with WebIDE so it can be added to a database table with the GUI.
The webIDE needs a few additional files and parameters so it can integrate your new field.
You will need a form and additional values in your configuration file.
Below is a sample form to be stored in the form folder with the exact same name as your Class.
In WebIDE create a new form called: **FieldUsPhone**
and enter the following code in the Row:
Label
[label]
Default Value
[default]
Turning on the field below will add International prefix and
extention forms field to the phone number mask
International
[international]
Extention
[extention]
You can select a style class for the form fields
CSS class Style
[css_form_class]
Now we will create a package and install it in the WebIDE.
If you are in a rush you can just copy the following files in the WebIDE:
class/FieldUsPhone.class.php
events/utils.formatUsPhone.inc.php
includes/FieldUsPhone.conf.inc.php
registry/FieldUsPhone.reg.xml
form/FieldUsPhone.form.xml
// Show or hide the WebIDE project
$cfg_show_WebIDE = false;
to
// Show or hide the WebIDE project
$cfg_show_WebIDE = true;
Then in the list of projects you should see the WebIDE.
Open the project and in the package section install our new **FieldType_UsPhone-0.1.pkg**
Then you should see the new field type when adding a Table Field or a Registry Field.