Development:Block formslib: Difference between revisions
mNo edit summary |
(Changed to reflect the simplified blocks forms) |
||
(9 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
(This is currently a proposal and isn't available for use). | (This is currently a proposal and isn't available for use see [http://tracker.moodle.org/browse/MDL-9641 MDL-9641]). | ||
= Overview = | |||
The block formslib functions are designed to allow developers easily create forms for their blocks. Please read [[Development:lib/formslib.php]] for full details about using formslib. | |||
= Creating a Global Configuration form = | = Creating a Global Configuration form = | ||
Line 6: | Line 8: | ||
In order to create a global confuration form using formslib, you must create a file <span class="filename">/blocks/BLOCKNAME/config_global_form.php</span> | In order to create a global confuration form using formslib, you must create a file <span class="filename">/blocks/BLOCKNAME/config_global_form.php</span> | ||
In this file a class needs to be created which follows the naming scheme: block_BLOCKNAME_config_global_form | In this file a class needs to be created which follows the naming scheme: block_BLOCKNAME_config_global_form and extends block_config_global_form | ||
In your form configuration class you need to implement the block_configuration() method | In your form configuration class you need to implement the block_configuration() method. Elements added will automatically saved in $CFG. | ||
<pre> | <pre> | ||
Line 17: | Line 19: | ||
function block_configuration(&$mform){ | function block_configuration(&$mform){ | ||
// add a html editor text field | // add a html editor text field named block_BLOCKNAME_text | ||
$ | $mform->addElement('htmleditor', 'block_BLOCKNAME_text', get_string('text','BLOCKNAME')); | ||
// set the default value | // This will set the default value of the textfield to 'mydefaultstring'. (If $CFG->textfield | ||
// | // exsts, it will be set to that when the form is loaded) | ||
$ | $mform->setDefault('mydefaultstring'); | ||
// | $mform->setType('block_BLOCKNAME_text', PARAM_RAW); | ||
$mform->setType(' | |||
$mform->addElement('text', 'block_BLOCKNAME_num', get_string('block_BLOCKNAME_num', 'BLOCKNAME')); | |||
$mform->setDefault('block_online_users_timetosee', '5'); | |||
// Add client side validation which forces the field as numeric | |||
$mform->addRule('block_BLOCKNAME_num', null, 'numeric', null, 'client'); | |||
$mform->setType('block_BLOCKNAME_num', PARAM_INT); | |||
} | } | ||
Line 32: | Line 43: | ||
</pre> | </pre> | ||
= Creating a Instance Configuration form = | |||
In order to create a global confuration form using formslib, you must create a file <span class="filename">/blocks/BLOCKNAME/config_instance_form.php</span> | |||
In this file a class needs to be created which follows the naming scheme: block_BLOCKNAME_config_instance_form and extends block_config_instance_form. | |||
In your form configuration class you need to implement the instance_configuration() method. Elements added will automatically saved in $block->config | |||
<pre> | <pre> | ||
<?php | |||
require_once $CFG->dirroot .'/blocks/block_config_instance_form.php'; | |||
class block_BLOCKNAME_config_instance_form extends block_config_instance_form { | |||
function instance_configuration(&$mform){ | |||
// add a text field named title | |||
$mform->addElement('text','title',get_string('configtitle', 'BLOCKNAME')); | |||
// this will set the form field to 'my title' | |||
// (if a title string is already stored in the block instance | |||
// configuration, it will automatically be set it to that) | |||
$mform->setDefault('title', 'my title string'); | |||
$mform->setType('title', PARAM_NOTAGS); | |||
} | |||
} | |||
</pre> | </pre> | ||
Latest revision as of 11:06, 1 May 2007
(This is currently a proposal and isn't available for use see [https://tracker.moodle.org/browse/MDL-9641 MDL-9641]).
Overview
The block formslib functions are designed to allow developers easily create forms for their blocks. Please read Development:lib/formslib.php for full details about using formslib.
Creating a Global Configuration form
In order to create a global confuration form using formslib, you must create a file /blocks/BLOCKNAME/config_global_form.php
In this file a class needs to be created which follows the naming scheme: block_BLOCKNAME_config_global_form and extends block_config_global_form
In your form configuration class you need to implement the block_configuration() method. Elements added will automatically saved in $CFG.
require_once $CFG->dirroot .'/blocks/block_config_global_form.php'; class block_BLOCKNAME_config_global_form extends block_config_global_form { function block_configuration(&$mform){ // add a html editor text field named block_BLOCKNAME_text $mform->addElement('htmleditor', 'block_BLOCKNAME_text', get_string('text','BLOCKNAME')); // This will set the default value of the textfield to 'mydefaultstring'. (If $CFG->textfield // exsts, it will be set to that when the form is loaded) $mform->setDefault('mydefaultstring'); $mform->setType('block_BLOCKNAME_text', PARAM_RAW); $mform->addElement('text', 'block_BLOCKNAME_num', get_string('block_BLOCKNAME_num', 'BLOCKNAME')); $mform->setDefault('block_online_users_timetosee', '5'); // Add client side validation which forces the field as numeric $mform->addRule('block_BLOCKNAME_num', null, 'numeric', null, 'client'); $mform->setType('block_BLOCKNAME_num', PARAM_INT); } }
Creating a Instance Configuration form
In order to create a global confuration form using formslib, you must create a file /blocks/BLOCKNAME/config_instance_form.php
In this file a class needs to be created which follows the naming scheme: block_BLOCKNAME_config_instance_form and extends block_config_instance_form.
In your form configuration class you need to implement the instance_configuration() method. Elements added will automatically saved in $block->config
<?php require_once $CFG->dirroot .'/blocks/block_config_instance_form.php'; class block_BLOCKNAME_config_instance_form extends block_config_instance_form { function instance_configuration(&$mform){ // add a text field named title $mform->addElement('text','title',get_string('configtitle', 'BLOCKNAME')); // this will set the form field to 'my title' // (if a title string is already stored in the block instance // configuration, it will automatically be set it to that) $mform->setDefault('title', 'my title string'); $mform->setType('title', PARAM_NOTAGS); } }