Use panel()->notify() in a widget?

This will add a message to the panel, but only after another refresh has been done. It’s one step behind.

panel()->notify('Success!');

How can I use it in a widget?

This is my case:

  1. I have a widget with a link to a route that does something.
  2. When the route has finished, it will redirect back to the panel.
  3. When I just throw in the code above it will not give the message right away. I need to refresh it again for it to show up.

Any ideas?

You need to redirect after the notifying:

panel()->notify('Success!');
panel()->redirect('/');

I saw the topic about that but then I need two redirects, one from the route to the panel and one to itself.

But maybe it’s the only way. Thanks! :slight_smile:

Or if your widget is sending an ajax request, you could do it with JavaScript. I remember seeing that Kirby exposes a JS function you could use.

But the panel itself uses redirection.

Yes, javascript would be a nice solution in some cases. :slight_smile: This time I will probably go for the double redirect anyway.

1 Like

Why two redirects? I’m using this a lot for the next version of Auto Git. The flow is:

  1. The user clicks a link on the widget.
  2. It hits a route.
  3. The route does it’s thing, notify and redirects back.

Although we are using the word “redirection”, the user won’t even notice since the route doesn’t output anything, which is totally fine.

My route is not aware of the panel class so I can’t add the notify there. Maybe I should check how you solved it in Auto Git.

This is my current implementation:

The widget template file:

if(get('pi') == 'install-success') {
	panel()->notify('Plugin installed!');
	panel()->redirect('/');
}

My route goes to this url:

/panel/?pi=install-success

It’s checks for a get variable to prevent an infinity loop.

Update

It works really well. I don’t even notice that two redirects are made. It feels like I’m always in the panel.