Hooks not working anymore?

I’m trying to use the hooks on the Kirby object to perform an action once a page changes. I did try to register it with this call:

$kirby->hook('panel.page.update', function() use(&$triggered) {
    trigger_error('fsdfdf');
});

Once I update a page it does not work. The function does not seem to be called. To debug it, I tried to use all different kind of variations for the hook name (like panel.page.* and * but they all don’t seem to work.

When I look at the source code and the commits, I see that the handling of hooks seemed to have changed in 2.4.0 (I’m using 2.4.1). This seemed to have caused problems before: panel.page.sort not firing as expected.

When I look into app/src/panel/models/page.php I see the different that in line 429 still the trigger( $event ) syntax is used. Is that correct? In line 271 (in move) the function is used like this:

kirby()->trigger('panel.page.move', [$this, $old]);

Is this just an inconsistency for me?

Nevertheless, also the panel.page.move seems not to be triggered at any time.

Does anyone have some hints how to debug this?

Thank you very much.

Best Regards
Tobias

Your syntax is not correct.

kirby()->hook('panel.page.create', function($page) {
  // your hook code
});

See docs.

Also, what is $triggered in your example?

The trigger_error function will not work with hooks, as they fail silently and do not return anything.

Thank you Sonja for your help.

$triggered is just a copy and paste mistake from me trying different things. The real hook looks like this:

$kirby->hook('panel.page.update', function() {
	trigger_error('Update trigger called');
});

Regarding the syntax I don’t see any difference? Even if this use(&$triggered) is there, it’s just PHP syntax and should not affect the execution of the hook.

I’ve seen that the hooks don’t return anything, but when I turn on error reporting and look into the log file, this errors should be reported anyway, right?

Thanks again for you help!
Tobias

it should read function($page)

Edit: you can test your hook like this:

kirby()->hook('panel.page.update', function($page) {
  f::write(kirby()->roots()->index() .'/log.txt', 'Worked perfectly!');
});

trigger_error() will not work.

I did create the log file first, change the mode so that everyone can write it and a plain f::write works just fine inside index.php of the panel.

Unfortunately inside the hook it still does not work.

By the way, have you defined $kirby? Otherwise use kirby(), as in my example.

Jep, I did. It comes from the site.php.

Have you tested this with a fresh starterkit?

I’m using Kirby (and the Panel) through composer, but I did already perform a fresh installation of all dependencies. Unfortunately nothing changed.

I’ll try to reproduce it with a fresh git clone now.

Have you managed to reproduce this issue with a fresh Starterkit?

Yes, I did. This is how I reproduced it:

  1. Downloaded the starter kit from http://download.getkirby.com/

  2. Updated the permissions on each folder and enable the panel inside the config.php.

  3. Created the log.txt inside the root folder and change the mode to be writeable by everyone

  4. Used f::write(kirby()->roots()->index() .'/log.txt', 'Worked perfectly!'); in the panel/index.php file to confirm that it writes to the file

  5. Removed the previous line and put this snippet into line 40:

    $kirby->hook(‘panel.page.update’, function($page) {
    f::write(kirby()->roots()->index() .’/log.txt’, ‘Worked perfectly!’);
    });

  6. Changed a file through the panel, no hook is being called

Maybe this helps, I’m using this version of PHP:

$ php --version
PHP 7.0.14 (cli) (built: Dec 23 2016 12:07:52) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
    with Xdebug v2.5.0, Copyright (c) 2002-2016, by Derick Rethans

What do you mean by that?

I needed to put c::set('panel.install', true ); into the config file as the panel would otherwise not be accessible.

line 40 of what file?

panel/index.php, my file then looks like this: https://gist.github.com/tobiastom/6038321615b2e0c07f96642dd239896d#file-index-php-L40-L42.

Sorry for all the trouble… :frowning:

Your hook should go into config.php, not the panel index file.

I put it into the panel file to make sure it’s executed. When I put it into the site/config/config.php file it is not called either.

Hint:

With https://getkirby.com/docs/installation/panel:

Please make sure to remove this line again after you have successfully created your first Panel user.

As far as I know, we haven’t had this issue before and I have no idea what could be causing this. I assume it it’s rather a server issue than related to Kirby. Does it work locally? Can you test with another server?

Just for future reference: somehow all my debugging attempts seem to be misleading. I can confirm that the hooks work as expected.

trigger_error does indeed not report anything inside hooks. f:write somehow only write partially to the file. Using file_put_contents directly make it possible for me to debug the hooks.

Just out of curiosity: why aren’t hooks reporting errors? This makes it especially hard to debug them.

Thank you @texnixe for your support and especially your patience!

Best Regards
Tobias