Hooks & Flash Message

Hooks

From the docs on registering hooks:

A hook is not expected to return anything. You cannot control the user flow with hooks at this state. They are silently triggered.

Does this mean that in a (hopefully) upcoming release we will be able to handle user flow then? I’d like to be able redirect certain types of users depending on the action taken.


Flash Messages

Is there some existing and/or recommended way for flash messages in Kirby? I’d like to be able to take an action and redirect the user to a page with a flash message of some kind letting them know what happened.

I haven’t used flash messages inside hooks so I had to try it. It can work, but not always.

kirby()->hook('panel.page.create', function($page) {
    panel()->notify('Your page has been created!');
    panel()->redirect('/');
});

If you try to notify without redirecting, you’ll only see Kirby smile notification: :)

With the redirection, the notification works. Just be aware that panel()->redirect() is relative to /panel.

There’s also panel()->alert() for red error messages.

2 Likes

Thanks @pedroborges!

If you try to notify without redirecting, you’ll only see Kirby smile notification: :)

I’m finding the smiley notification not very helpful. Even a simple check mark would be more helpful at the very least. And if you set a “notify()” message, the panel should use that message whether you leave the page or not. Just my opinion.

Just be aware that panel()->redirect() is relative to /panel.

So is there anyway to redirect the user to a specific page on the front-end?

1 Like

I just tested it. How cool! :slight_smile:

kirby()->hook('panel.page.create', function($page) {
  panel()->alert("Error!! Just kidding!");
  panel()->redirect('pages/' . $page->id() . '/edit');
});

This should hopefully always redirect to the correct page after creation.

For that you can use the go('page-uri') Kirby’s helper function.

I think this isn’t intentional. What happens is that your message is set before Kirby’s. So the last one gets displayed.

@pedroborges I was trying to redirect user after updating their account in the panel but go() didn’t seem to work. Does it work for you?

go() is what the panel()->redirect() uses under the hood, but it seems to handle ajax requests differently. Maybe the user update send an ajax request…

Yes,go() does not work in the Panel.