This is great news and possibility to create any field type you want on plugin base. And that is very simple.
First let's see an installation XML file.
<?xml version="1.0" encoding="utf-8"?> <install version="1.5" type="plugin" group="joomsuite_content_fields"> <name>Content Field - Text</name> <author>JoomSuite</author> <creationDate>May 2008</creationDate> <copyright>Copyright (C) 2005 - 2008 JoomSuite. All rights reserved.</copyright> <license>Commercial</license> <authorEmail>support@mightyextensions.com</authorEmail> <authorUrl>www.mightyextensions.com</authorUrl> <version>1.0</version> <description> Plugin for Resources component add new field type (Text) </description> <files> <filename plugin="text">text.php</filename> </files> <params> <param name="short" type="radio" default="0" label="Simple Mode on frontend" description="Show only general toolbar butons with editor"> <option value="0">No</option> <option value="1">Yes</option> </param> </params> </install>
Please, pay attantion on group name in line 2 also on plugin name in line 13.
This name will be considered as name of new field type. You can add any parameters
you want and late you can use it to create more flexible and addoptive field types.
The Plugin system have 3 main events triggered.
onRenderField
This event is triggered on creating fields on article create/edit form.
function onRenderField( &$obj, &$params, $type) { if($type != 'text') return; ?> <input type="text" style="width:<?php echo $params->get('field_size')?>" name="fields[<?php echo $obj->id?>]" value="<?php echo stripslashes($obj->field_value) ?>"/> <?php }
This event do not return anything but echo HTML directly. Please, pay attention on line 3.
This line should be first in every event you only need to change 'html' to plugin name you
set in installation XML on line 13.This event receive 3 parameters.
- $obj - Is an object that contain field information. To create this object this query was used
SELECT f.*, v.`field_value` FROM `jos_js_res_fields` AS f LEFT JOIN `jos_js_res_record_values` AS v ON v.`field_id` = f.`id` WHERE f.`published` = 1;
So to find out what properties this object have look that table.
But I believe that you will need only 'id' and 'field_value' - $params - is a field params that user set on field creation (not on plugin editing)
- $type - name of the plugin actually
Also not that to get $value of this field to next event automatically you need
to name field as in line 6 (fields[$obj->id]).
Example:
echo "<input type='text' name='fields[" . $obj->id . "]'>";
In any case if you create your own name it is smart idea to use ID of the field in the
name to make it unique and you will need to get value using JRequest as it will not be passed to event.
Line 5 set size to field. Size is the global parameter for field. $params->get('field_size')
return value even if you not created this parameter for plugin. Do not forget to use it.
By the way as one of the idea, you can change field views on front-end
and back-end using $mainframe->isAdmin().
onBeforeSave
This event is triggered before storing field value to DB. In this event you can
get$value of your field or set of fields, and prepare it to store in DB.
function onBeforeSave(&$value, &$params, $type, $id, $row, &$error) { if($type != 'html') return; if(!$value) return; $db = JFactory::getDBO(); $text = stripslashes($value); $value = $db->getEscaped(str_replace( '<br>', '<br />', $text )); }
This function receive 6 parameters.
- $value - Is an value of the field. Value may not be passed as content not sure
how you named your field but everything you set to this parameter will be stored
as field value. Value will be passed only if you named your field as recommended
in onRenderField event description. Other case you may need to use JRequest
to get initial field value. - $params - is a field params that user set on field creation (not on plugin editing)
- $type - name of the plugin actually
- $id - ID of the field. Usually used to identify or get value of other fields that
was named using $id to make name unique. - $row - is an object of just saved record. You can use $row->id or $row->user_id
to create folder to store file or make other actions. - $error - is a trigger for producing error. If you check value and it is not correctly
formatted you may do like this
if(!in_array($ext, $exts)) { $msg = JText::_("File is not apropriate format"); $msg .= "<br>File: ".$name; $msg .= "<br>Allowed Formats: ".implode(", ", $exts); JError::raiseWarning(403, $msg); $error = 1; return; }
This example check if extension of uploaded file is correct and display
error and open form for correcting entered information.
Note everything you set to $value variable will be stored to DB. You do not need to return anything.
By the way, best way to store arrays is to path it through serialize() function.
onRenderFieldValue
And this is most simple event, that render field on frontend in article view.
function onRenderFieldValue(&$out, &$field, &$params, $type) { if(!$field->field_value) return; if($type != 'select') return; $val = explode("^", $field->field_value); if(@$val[1]) $color = ' style="color:'.@$val[1].'" '; else $color = ''; $out = "<span{$color}>{$val[0]}</span>"; }
This function receive 4 parameters.
- $out - This is variable you should set complete HTML text that you
want to be displayed. - $field - This is object contain field information. I think that only useful here is
$field->field_value that contain value you saved in previous event. - $params - This is a field params that user set on field creation (not on plugin editing)
- $type - This is the name of the plugin actually
So everything you set to $out variable will be rendered as field value.
You do not need to return anything.
Construction
And finaly constration notes and full plugin example.
<?php defined( '_JEXEC' ) or die( 'Restricted access' ); jimport( 'joomla.plugin.plugin' ); class plgJoomsuite_content_fieldsText extends JPlugin { function plgJoomsuite_content_fieldsText( &$subject, $params ) { parent::__construct( $subject, $params ); } function onRenderField( &$obj, &$params, $type) { if($type != 'text') return; ?> <input type="text" style="width:<?php echo $params->get('field_size')?>" name="fields[<?php echo $obj->id?>]" value="<?php echo stripslashes($obj->field_value) ?>"/> <?php } function onBeforeSave(&$value, &$params, $type, $id, $row, &$error) { if($type != 'text') return; if(!$value) return; $db = &JFactory::getDBO(); $value = $db->getEscaped(trim($value)); } function onRenderFieldValue(&$out, &$field, &$params, $type) { if($type != 'text') return; if(!$field->field_value) return; $out = $field->field_value; } } ?>
Above is most simple text type plugin. Please, note plugin name
plgJoomsuite_content_fieldsText. It is constructed of 3 parts
plg - Joomsuite_content_fields - Text which actually prefix - group name - plugin name.
Also function with the same name should be created for proper class construction on plugin call.
I hope you now can see endless posibilities and options that you can create for JoomSuite Content.
on
on 
+1 (209) 800 1209