Redirection to plugin from subplugin

Redirection to plugin from subplugin

by Ivana Skelic -
Number of replies: 5

Hi, I'm having problem with redirection from sub-plugin to plugin. What I want to do is this: if the form is canceled redirect and/or if the data is entered in the form redirect as well. Somehow I keep getting this error no mater what I do 

A required parameter (chapterid) was missing

More information about this error

Debug info: 
Error code: missingparam
Stack trace:
  • line 463 of /lib/setuplib.php: moodle_exception thrown
  • line 545 of /lib/moodlelib.php: call to print_error()
  • line 30 of /mod/book/tool/validator/bcindex.php: call to required_param()

this is line 30 code:

$chapterid = required_param('chapterid', PARAM_INT);

And the lines in which I'm using redirection:

if ($mform->is_cancelled()) {
if (empty($chapterid)) {
redirect($CFG->wwwroot . "/mod/book/view.php?id=$cm->id");
} else {
redirect($CFG->wwwroot . "/mod/book/view.php?id=$cm->id&chapterid=$chapterid");
}
} else if ($data = $mform->get_data()) { ... ... redirect($CFG->wwwroot . "/mod/book/view.php?id=$cm->id&chapterid=$chapterid"); ... }

 

Can anyone suggest me what am I doing wrong here?

 

Average of ratings: -
In reply to Ivana Skelic

Re: Redirection to plugin from subplugin

by Darko Miletić -

If you can not guarantee presence of chapterid in every page call than make that parameter optional instead of required.

 
$chapterid = optional_param('chapterid', 0, PARAM_INT);

if ($chapterid == 0) {
  // no chapter do something here
} else {
  // regular code
}

 

 

In reply to Ivana Skelic

Re: Redirection to plugin from subplugin

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

If you need the 'chapterid' param, then you need to make sure it is included within the form you have created, otherwise, when you submit the form, the chapterid value will not be sent to the page.

Within your form definition you will need to add something like:

$mform->addElement('hidden', 'chapterid');

You will also need to pass this value into the form, via the $form->set_data($dataobject); call or using the second, 'customdata' param when initialising the form instance.

 

In reply to Davo Smith

Re: Redirection to plugin from subplugin

by Ivana Skelic -

Actually it is included. The form shows with no problem (the form inherits form from the plugin) but when I click on form buttons (save or cancel) I get this.

In reply to Ivana Skelic

Re: Redirection to plugin from subplugin

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

If there is a hidden element within the form definition called 'chapterid', then the 'required_param('chapterid', PARAM_INT)' call will find it.

You should double-check within your browser (view source) that this hidden input exists within your form and that a value is being submitted for it when you click on one of the buttons in your form (e.g. using the chrome developer tools 'network' tab to check the values being sent).

Average of ratings: Useful (1)
In reply to Ivana Skelic

Re: Redirection to plugin from subplugin

by Ivana Skelic -

It seems to me that I'm moving on with this one. What I've done is very silly mistake but didn't get it until now. I instantiated the form inside of if clause and that caused the mess. Now I've moved it outside and have some minor issues to fix. Hope it will do fine.