Use plugin's form in subplugin

Use plugin's form in subplugin

by Ivana Skelic -
Number of replies: 3

Hi everyone,

I'm new in Moodle development and trying to develop subplugin for Book module. My question is is it possible to use plugin's form (edit_form in particular) in subplugin, ie is it possible to instantiate that form in subplugin?
What I want to do is to create custom validation subplugin and use the editor from the plugin for editing content.

 

Thanks!

Average of ratings: -
In reply to Ivana Skelic

Re: Use plugin's form in subplugin

by Darko Miletić -

You have a great degree of freedom in developing code for Moodle. So yes, you can use that class if you need it. It is however considered a good programming practice to extend class you plan on using because it than later gives you chance to easily add any new stuff specific to your plugin.

 

 
class myform_sample extends moodleform {
    protected function definition() {
        $mform = $this->_form;

        $options = array(
            'startyear' => 2013,
            'stopyear'  => 2020,
            'timezone'  => 99,
            'optional'  => false
        );
        $mform->addElement('date_selector', 'vreme', 'aa', $options);
        $this->add_action_buttons();
    }
}


class myform2 extends myform_sample {
    protected function definition() {
        $mform = $this->_form;
        // Add custom stuff before.
        parent::definition();
        // Add custom stuff after.
    }
}
Average of ratings: Useful (1)
In reply to Darko Miletić

Re: Use plugin's form in subplugin

by Ivana Skelic -

Hvala Darko!

And I suppose I should include plugin's class definition with 

require_once(dirname(__FILE__).'../../edit_form.php');

In reply to Ivana Skelic

Re: Use plugin's form in subplugin

by Darko Miletić -

Nema na čemu smile

In Moodle you should avoid using relative paths. Always use absolute path like this:

global $CFG;
require_once($CFG->dirroot.'relative path of module starting with / ');
require_once($CFG->dirroot.'/blocks/myblock/myform.php');

 

Average of ratings: Useful (1)