Adding custom action button in Panel

Copied over from the old forum: http://getkirby.com/forum/panel/20150228/adding-custom-action-button-in-panel

Looking for solution - in Panel (in blueprint page) need button similar as built-in SAVE button, but with different action - maybe someone can advice where to start. Or have some example.
Will try to make my own by adding button under “fields” folder. But not sure if button qualifies as “field”.

If I am more specific - button will generate site content as JSON and will save JSON file to local folder.

Any lead or idea will be much appreciated :wink:

I want to implement a button that starts a bash script which works on specific uploaded files.

Any ideas?

Adding a field type “Generate JSON button” in the “fields” folder sounds like a good angle on this specific problem.

That field type should display a button and maybe trigger AJAX-like the actual script to generate the JSON on click.

Would be my best guess - or you fork the panel and try to work it in there directly. But that sounds like much more work.

1 Like

I will play around with a suggested button field. Thanks.

Looking forward to it, let us know if You have any news about adding custom button to panel.

Hmm, topic is changed to SOLVED, but actually still struggling with problem and there is no real solution yet - can not add custom button next to SAVE button in Panel…

For that you would need to change the panel core…

Changing Panel core sounds too complex for me :cry: then we left hope on some smart guys or Godfather Bastian.

1 Like

BenSeitz: You are right. Was playing around with “fields” / blueprint and noticed - radiobuttons, checkboxes etc states are checked (fired) only when pressing “SAVE” button. I was able to call “Hello World” in console, when pressing “SAVE”. So right now Im thinking not to adding additional extra button in Panel, but just add function (call extra action), when pressing SAVE button.

Of course possibility to add extra button would be good anyways, but right now just looking for easiest ways to reach my goal - generate all contents JSON string (and save locally) with button click in Panel.

As I said earlier:

Custom form fields allow for their own css and javascript: https://getkirby.com/docs/developer-guide/panel/fields

So you could simply create a custom form field that consists e.g. of a simple span-element and first style this one through a custom css file to look like a button.

And then you bind a javascript action through a custom js file to wait for a click on the button and on that click call your script e.g. through the ajax function. I assume (haven’t tested) that you could use jQuery as it comes with the Kirby panel anyways?

As long as you are just trying to start another script, that shouldn’t be too hard. Gets more complicated if you want to pass any values from the other panel fields.

And if all of this sounds too complicated or too much hacking, then I’m afraid you just have to wait until Kirby implements hooks or similar things for the panel (which it probably will, but who knows when exactly).

1 Like

@distantnative has the way of it. That’s how I would do it too.

It is a slightly more convoluted way of producing what you are after, but you could always add a separate route into your kirby config that has a function to return the json response for a uri. Fiddle with the headers and you can happily send this as a download too.

Please note that I haven’t tested this, but a quick mock up gives me:

c::set('routes', array(
    array(
        'pattern' => 'json/(:all)',
        'action'  => function($uid) {
            $page = page($uid);
            $jsonArray = array_map(
                function($item) {
                    return $item->value();
                },
                $page->content()->data());
            return response::json($jsonArray);
        }
    )
));
2 Likes

Thank You @distantnative and @walkerbox for guidelines!

But figured out a bit different way - so got it work. My approach:

  1. Added file “site\fields\action\action.php” (which is copy of panel\app\fields\info\info.php)
  2. Inside action.php changed result function, now looks like this:
public function result() {
    $url = 'http://' . $_SERVER['HTTP_HOST'] . '/mysite/api';
    $obj = file_get_contents($url);
    file_put_contents('../../api.json', $obj);
  }

and in bluprint added:

fields:
  action:
    label:
    type: action
    text:

So everytime I something changing in content and push SAVE button afterwards, all content data is saved as JSON.
How to set up JSON export I wrote here: Export all site content as JSON

Why I using such approach with JSON? because I have offline site and don’t want to bother with PHP requests every time. So I have solid data as JSON, but its flexible on every Panel SAVE.

1 Like

The link does not work anymore.

@Svnt fixed (and some more chars)

1 Like

i recently published my Opener Panel Button Plugin which allows you to trigger json responds from the panel. but setting up a controller/template to call the bash script still needs to be done.

2 Likes