code200.miha
code200.miha

What is the best way to add custom button / link to plugin settings page?

I have a plugin that caches a lot of stuff. I would need a button that user can click and plugin cache would get removed.

Any suggestions?

Thanks!

alxy
alxy

You could add a partial to the form that triggers an ajax request to some other endpoint (probably a controller).

code200.miha
code200.miha

Thanks @alxy for pointing me in right direction.

However I am checking docs here https://octobercms.com/docs/backend/controllers-ajax#ajax-handlers

And I am unable to get it to work. Where do I put onMethod?

plugins/Code200/imageking/models/settings/fields.yaml

clear_cache:
            tab: General
            type: partial
            path: $/Code200/imageking/controllers/cache/_clear_tmp_files.htm

plugins/Code200/imageking/controllers/cache/_clear_tmp_files.htm

<button class="btn btn-danger" type="button" data-request="onClearTmpFiles">
Clear tmp files
</button>

plugins/Code200/imageking/controllers/Cache.php

<?php
namespace Code200\ImageKing\Controllers;

use Backend\Classes\Controller;

class Cache extends Controller
{
    public $requiredPermissions = ['acme.blog.access_posts'];

    public function index()
    {
        //TODO
    }

    public function onClearTmpFiles() {
        die("not working :((");
    }
}
alxy
alxy

In this case you will need to create a widget, because you don't have access to the controller that handles the settings page (its in core).

code200.miha
code200.miha

Thanks, I think I got it working with widgets :)

Anyone else searching for answers check here:

p.s. It would be welcomed if we could add comments to documentation :)

Last updated

1-5 of 5