Wednesday 22 February 2017

Moodle: moodle_form inside module: Edit or new?

Hello,

Sometimes inside a custom Moodle module we need to know if we are editing or creating a new module (activity or resource) instance... Here the solution:

function definition() {
        global $CFG, $DB;
        $mform =& $this->_form;

        if (is_numeric($this->_instance) && $this->_instance && $mymodule = $DB->get_record("mymodule", array("id"=>$this->_instance))) {
            $mform->addElement('header', 'general', 'Editing');
        } else {
            $mform->addElement('header', 'general', 'Creating');
        }

...

Best regards,
Iban Cardona.