Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

Block formslib

From MoodleDocs
Revision as of 20:06, 29 April 2007 by Dan Poltawski (talk | contribs)

(This is currently a proposal and isn't available for use).


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.

In your form configuration class you need to implement the block_configuration() method, adding the elements you need with $this->add_config_element(). Using this method means the form elements will be processed when the block configuration is saved.

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
        $this->add_config_element('htmleditor', 'block_BLOCKNAME_text', get_string('text','BLOCKNAME'));

        // set the default value, this will set the default value
        // or set the $CFG value for the same name, if it exists
        $this->set_config_default('block_BLOCKNAME_text', 'mydefault');

        // set an appropiate PARAM type
        $mform->setType('block_BLOCKNAME_text', PARAM_NOTAGS);

    }

}


Custom formslib-style functions

add_config_elment

This does the same as addElement, except that is also adds the element to the list of form elements which need to be saved by the block configuration

set_config_default

set_config_default($name, $default = NULL)

This will try and set the the element named to its stored value ($CFG->$name, in the case of global configuration, $block->config->$name in the case of instance configuration).

If there is no stored value, the it will set the default to $default (if it exists)