Jump to content

Basic hook question...


celfred
 Share

Recommended Posts

Hello,

I am getting nuts trying to understand hooks and I hope someone in the community will be able to help. This is deiving me crazy ! I have tested tens of possibilities to eventually reduce my code to this :

  bd('outside');
  $wire->addHookAfter('Page::render', function($event) {
    bd('inside');
  });

And if someone could tell me why my bd('inside'); never triggers... I would be infinitely grateful !

EDIT : Forgot to say : this piece of code is in my _init.php included in my template (but I've also tried in my site/ready.php for no better results...)

Link to comment
Share on other sites

Well... I have this in the preceding lines which seems to work ok :

	  $wire->addHook('LazyCron::everyDay', null, 'checkActivity'); // Check all players activity
  $wire->addHook('LazyCron::everyDay', null, 'randomSpecial'); // Set random special monsters
  $wire->addHook('LazyCron::everyDay', null, 'emptyTmp'); // Empty /tmp subtree every night
  $wire->addHook('LazyCron::everyDay', null, 'cleanTest'); // Init test-team players
	

Is there a basic hook other that Page::render which I could test with ? (I must admit I'm getting lost in all those hooks, hence my post in the 'Get started' forum ? )

For info, I'm with PW 3.0.62.

Thanks for the help !

Link to comment
Share on other sites

Ah... This TracyDebugger is indeed extraordinary ?

There might be a clue here : my Page hooks are in lesser number than yours (no 'if', no 'links').

If I use Page::path, my 'inside' gets triggered, but as soon as I use Page::render, nothing happens ? And why is Page::render not listed our tables (only renderField or renderValue) ?

 

Capture du 2019-03-13 16-49-00.png

Link to comment
Share on other sites

Those numbers are line numbers in the Page.php file. It suggests you are running an older version of PW.

Page::render is a unique hook in that it actually comes from the PageRender.module file.

image.thumb.png.852b518097fb86c98c65bd08eeacbc89.png

Perhaps you could try hooking into PageRender::renderPage and see if that works.

Link to comment
Share on other sites

10 minutes ago, celfred said:

Ok. I have the same as you then in PageRender.module, but... hooking into PageRender::renderPage keeps me 'outside' ? 

What page are you viewing to test this? I don't think it should matter, but just curious.

Other than that, I wonder if you can try updating to the latest PW version, just in case.

Link to comment
Share on other sites

I'm viewing my homepage on localhost (the page can be seen there), but actually I went on many other pages to test, checked cache and so on... but no change...

2 minutes ago, adrian said:

Other than that, I wonder if you can try updating to the latest PW version, just in case.

I was just thinking about this. But I'm afraid I won't be able to do that right now. I always get scared of such a major update in case I break things ?  I prefer having more time ahead of me to do such an action. I'm not trusting my dev abilities !

So I stay tuned for ideas and I'll keep you updated when I update. But thanks a lot for your help @adrian.

Link to comment
Share on other sites

Just a thought: could there be a before hook on Page::render (perhaps from a 3rd party module) that replaces the original function and prevents after hooks from running? You can see all active hooks with trace with that little snippet:

$allHooks = $hooks->getHooks($page, 'render');
bd($allHooks);

Make sure to visit a page on the frontend, otherwise ProcessPageRender won't be invoked and Page::render won't even exist (at least that's the case here when testing on 3.0.96.

  • Like 1
Link to comment
Share on other sites

Thanks again, but nothing changes...

So you know :

  • I disabled FrontEndEdit module
  • I cleaned all my files in the assets/cache/folder
  • I updated on my local site to PW 3.0.123

And my Page::render hook is still ignored. I tried Page::loaded and this one works well (I get my 'inside' message).

This is driving me crazy ? 

Link to comment
Share on other sites

Indeed Tracy allows to easily disable modules. I did check them all and... no change...

Regarding a fresh install, that sounds like a huge task to me : reinstall a new database, reinstall the different modules I use, re-import data... and how will I put that back to my remote site if it solves the issue ? I'm speechless ? 

I've just started looking at the 'Export Profile' and trying to follow the instructions on a new local installation. But it fails. I get many errors creating the database (pa.pages does not exist, pa.modules does not exist...) and it freezes. I guess my files are too large... And I'm wondering if that's the proper way to do things I have a feeling doing an export on a new install will export the problem as well, no ? I tend to think I'll have to do all steps I've mentionned in my preceding paragraph one by one (can you confirm this is what I should do ?) but then that would take me a very long time... So I would tell you in a few days (or even weeks ?)

I can't understand how I got into this situation ? Anyway, thanks a lot for your help and as I said, once you'd confirm I have to re-install things 1 by 1, I will get to it at my possible pace ? 

Link to comment
Share on other sites

Sorry, I wasn't suggesting setting everything up again - just a clean PW install with no modules, pages, templates etc just to see if it works. I imagine it will, but thought it might be a good sanity check in case there is some weird server setting somehow interfering - although I can't imagine what and you mentioned that you tried on your local dev as well and it still doesn't work.

 

  • Like 1
Link to comment
Share on other sites

7 hours ago, celfred said:

EDIT : Forgot to say : this piece of code is in my _init.php included in my template (but I've also tried in my site/ready.php for no better results...)

The hook code definitely needs to be in /site/ready.php or /site/init.php. It won't work in your template-prepended _init.php file - the page is already rendering by the time that code executes.

  • Like 3
Link to comment
Share on other sites

@Robin S : OK ! I'll test during the day. Actually, I think I did test in site/ready.php but I was wondering if bd() would work there and I imagined then that I had no simple way to see if everything worked (see my dev level !). Since I already had a couple of working hooks in templates/_init.php, I used it again here...

So most of my last tests (disabling all modules for example) need to be re-tested in site/ready.php. I'll do this during the day and tell you how this is going.

@adrian Oh ok; Then, I'll do that also from a blank-site profile.

Thanks a lot !

Link to comment
Share on other sites

I'd also place a bd('render was called') directly in the core's render function to see if it gets executed. Or a die('render') there, whatever you want. Just to make sure the method is actually called (similar to what BitPoet said).

Link to comment
Share on other sites

4 hours ago, celfred said:

I think I did test in site/ready.php but I was wondering if bd() would work there

bd() will work in site/init.php and site/ready.php - your OP code works for me in both of those files. You mentioned you had tested there already, so I didn't think about it, but I think @Robin S might be correct about what the problem is.

Link to comment
Share on other sites

Back here with... quite an amount of shame...

I'm sorry I have taken time from you all. Here's the thing : my site/ready.php which I thought did not have access to bd() because I couldn't even see a simple bd('ok'); was not at the right place. A bell just rang in my head a few minutes ago after making a fresh re-install of a blank profile ! I came back to my original local site and thought : "No way, Fred ! site/ready.php NOT your-website-root/ready.php !!!!" I moved ready.php and.... tada ! (Of course...). What a nerd...

So again, SORRY for having taken some of your time, but thanks for your helpful answers. I still have learned quite a lot from our exchange and that is the most important thing.

I'm glad I posted in 'Getting started' ;)

  • 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

×
×
  • Create New...