Jump to content

back to the roots: problems with simple hooks :(


bernhard
 Share

Recommended Posts

hi everybody,

i have a weird problem regarding hooks. there was some unintended results so i started to make it really simple and see what's happening. this is what i came up with:

i have this simple hook inside /site/ready.php

$this->addHookAfter("Pages::saved", function($event) {
  $this->log->save('debug', 'saved hook');
});

when i save any page in the admin i get a new log-entry. so far, so good...

when i put this inside my home.php template and visit my frontpage:

home.php
<?php
$page->of(false);
$page->title = 'set by template';
$page->save();

...it changes the page title and runs the hook (i get a new log entry).

but when i use @adrian s console of tracydebugger with this code:

$p = $pages->get(1);
$p->of(false);
$p->title = 'set by API';
d($p->save());

...it returns TRUE, it saves the page, but it DOES NOT call the hook. it does not save a new log-entry.

I wasted more than an hour now to find out what's going on here. please could anybody of you bring light into that weird problem? :( 

in the example it is only a log-entry, but i have several saveReady or added hooks that seemed to work but actually didnt :( for example it does NOT activate languages for pages that i created from the API (https://processwire.com/talk/topic/4383-how-to-set-language-active-via-api/?do=findComment&comment=147564).

that's really critical problems. am i totally misunderstanding anything?

thanks for your time!

  • Like 1
Link to comment
Share on other sites

Hi @bernhard - I am not really sure why at the moment, but if you move your hook to init.php (instead of ready.php), it works when the code is run from Tracy's console panel. So it looks like for some reason ready.php is not being called from the ajax call that the console panel makes when running code.

Does that help?

  • Like 1
Link to comment
Share on other sites

To follow up more - I just tried this in init.php:

bd('test init');

$this->addHookAfter("Pages::saved", function($event) {
    bd('test init inside hook');
      $this->log->save('debug', 'saved hook');
});

and this in ready.php:

bd('test ready');

$this->addHookAfter("Pages::saved", function($event) {
    bd('test ready inside hook');
    $this->log->save('debug', 'saved hook');
});

and got this response when running from the console panel:

596f9d51b1df9_ScreenShot2017-07-19at10_56_15AM.png.eb4f05946a3d30e74ac29629c39b5858.png

So, ready.php is being called, but hooks inside it are not - weird :)

  • Like 1
Link to comment
Share on other sites

thank you adrian, i can confirm this behaviour!

it turned out that my problem was a combination of two

1) like you mentioned the hook does not get called when called from the tracy console - that's weird and i think that's somewhat critical as it could lead to problems (not doing things that you would expect, doing things that you would not expect; and hard to see sometimes!).

2) even in the init my language hook didn't work. turned out that this works:

  // set all languages active automatically
  $wire->addHookAfter('Pages::added', function($event) {
    $page = $event->arguments(0);
    foreach ($this->wire->languages as $lang) $page->set("status$lang", 1);
    $page->save();
  });

while this does NOT:

  // set all languages active automatically
  $wire->addHookAfter('Pages::added', function($event) {
    $page = $event->arguments(0);
    foreach ($this->wire->languages as $lang) $page->setAndSave("status$lang", 1);
  });

no idea what could be the reason for this :(

 

#2 is somewhat offtopic. but what do you think about the tracy hook problem? do you think this problem is more related to tracy?

my init.php

bd('init.php');

  // test tracy
  $wire->addHookAfter('Pages::saveReady', function($event) {
    $page = $event->arguments(0);
    bd('saveReady in init.php');
    bd($page->id);
  });

my ready.php

bd('ready.php');
  
  // test tracy
  $wire->addHookAfter('Pages::saveReady', function($event) {
    $page = $event->arguments(0);
    bd('saveReady in ready.php');
    bd($page->id);
  });

after loading:

59705d0070070_2017-07-2009_32_50-.png.b7006682582995e01b07bb5322b191d4.png

after executing the code inside the console:

59705cff01b45_2017-07-2009_33_23-PagesProcessWire360v2_dev.thumb.png.a1d6a417b4ac25299dc4e24a32ca5e8b.png

  • Like 2
Link to comment
Share on other sites

@adrian do you think you can have a look why the hooks inside ready.php are not applied when executed from the tracy console?

i had an issue again today... i wanted to test a simple property hook and got an error. i think that's quite a big issue because imho the panel code should just execute as if it were executed inside a template or module. and i guess testing hooks, functions, creating pages via the console etc. is a VERY common usecase for tracy-users...

  // get group of given fbrole
  $wire->addHookProperty('Page::group', function($event) {
    d('test');
    $page = $event->object;
    if($page->template != 'fbrole') return;

    $event->return = $this->pages->get(1634);
  });

log (first from ready.php, then from init.php):

597312ef93322_2017-07-2210_54_24-PagesProcessWire360v2_dev.png.d6ea2c0daf09193782ca21ce2b992454.png

  • Like 2
Link to comment
Share on other sites

Hey @bernhard - I spent quite a bit of time looking into this the other day with no success. It turns out it's not just hooks inside ready.php - it seems like ready.php is never called at all from the Tracy console panel. I will investigate further, but so far a bit of a mystery. I thought perhaps it was an issue with ajax calls (which the console panel uses), but a normal ajax call results in ready.php being called, so really not sure at the moment. I'll see what I can figure out.

Cheers,
Adrian

  • Like 3
Link to comment
Share on other sites

On 7/23/2017 at 2:35 AM, bernhard said:

Thank you! That sounds like a big issue... And very strange. Hope you find it out soon :)

Hey @bernhard - I just PM'd you with a new version that should work as expect with ready.php and hooks inside it. If you can test for me, hopefully I can get a new version released.

Thanks!

  • Like 3
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...