Making a form with a single button in a plugin

Making a form with a single button in a plugin

by Richard Müller -
Number of replies: 5

Hi everyone.

I've developed a block plugin that displays some information which is important for our school.

Now I want to add a feature where the user can switch between two different display styles. The choice of the user should be saved on the server so that the next time the user views the block, it will already be displayed correctly.

Ideally, there would be a single plain button (or even better: a "rocker switch" like the one used for switching to edit mode in moodle 4.0) to toggle this preference.

I've looked into the Moodle Form API, but it seems that I would need one field for selecting the preference and then a submit button, which would be too bulky for the block. Since the 'No submit button' option from the Form API apparently gets called every time that the form gets constructed, I can also not use that one.

So how can I add a single button which updates a user preference (which I'll probably save in a database table of the plugin) and then reloads the page?

Average of ratings: -
In reply to Richard Müller

Re: Making a form with a single button in a plugin

by lior gil -
Picture of Core developers

The edit switch mode isn't created through the form API but from a template (see /llib/templates/editswitch.mustache).

You can build your own form, either by code or a custom template.

Or, if you're looking for a more standard form, you can use the built in single button form by calling $OUTPUT->single_button().

Average of ratings: Useful (2)
In reply to lior gil

Re: Making a form with a single button in a plugin

by Richard Müller -

If I use the single_button, what should I specify as URL and params, if I want to trigger a reload of the page?

Just using an empty string as URL works, but it will ignore all previously used params. On the dashboard, that is fine (I think). But if you're in a course, this will not send the id (courseid) which will result in an error. So should I simply check if the user is currently on the dashboard or in a course and for the latter, manually add a param "id" to the request? Or is there a better way to do this?

In reply to Richard Müller

Re: Making a form with a single button in a plugin

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

On any given page, the following should get the official URL (with any params that are required to correctly display the page):

global $PAGE;
$pageurl = $PAGE->url;

If you need to add some extra params to this (to trigger the action of the button when submitted), you could handle it like this:

$actionurl = new moodle_url($PAGE->url, ['action' => 'myaction']);


Average of ratings: Useful (3)
In reply to Davo Smith

Re: Making a form with a single button in a plugin

by Reefo Relaxo -
Hi Davo, maybe you could help me? Is it possible to use single_button inside a form ? i'm trying to add a button below the add_action_buttons. Is that possible ?