Trigger hook does not fire $oldPage

I setup a hook.

kirby()->hook('*', function($page, $old_page = null) {
    echo $old_page;

I test it with this:

kirby()->trigger('', page('projects/project-a'));

I expected $old_page to be the same as $page would output:


Instead it did not output anything. To make it work well with my test environment it would be nice to get it working.

Any ideas? Reasons for it, how to get around it? Can I force some arguments to it to make the objects different?

(Kirby 2.5.5)

(quick reply)

There is a change in this behaviour in Kirby 2.5.6 RC -> could you quickly check with this version?

No, there was no difference. It does not even return the $old_page object for some reason. I’m working around the problem by calling another method with the same arguments. That way I can test the function without even trigger the hook.

By the way, the hook output $old_page when triggered by the panel, but not by the trigger function.

My workaround solution now looks like this:

kirby()->hook('*', function($page, $old_page = null) {
    $Hooks = new Hooks();
    $Hooks->hook($this->type(), $page, $old_page);

To test my hook, I need to do this:

$Hooks = new Hooks();
$Hooks->hook($this->type(), $page, $old_page);

When the Panel triggers a hook, a Panel page object is passed.

kirby()->hook('', function($page, $oldpage = null) {
  error_log(get_class($oldpage)); // ==> Kirby\Panel\Models\Page
1 Like

Good to know, thanks! :slight_smile: I’ll contine with my workaround.

I also noticed that if I try kirby()->trigger() to test multiple hooks, it only run the first one. The rest is skipped. Therefor I can’t run an update hook and then a delete hook directly after. Maybe it’s to prevent unlimited loops.

Thats exactly why it is only triggered once. But if you loop through pages and need to trigger the correct events for each page you can reset the triggered array in the loop.

// Reset previously triggered hooks
kirby()::$triggered = array();

This might not be safe for some use cases! I used it for a copy paste action for multiple pages and in this case it is safe to use.

1 Like