Jump to content
celfred

Basic hook question...

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...)

Share this post


Link to post
Share on other sites

No problem here with your code.

image.png.ef7fc0520d6d86ed908d4cff38df38a0.png

I'm actually not sure what to suggest at the moment.

Do other hooks behave any better? 

  • Like 1

Share this post


Link to post
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 !

Share this post


Link to post
Share on other sites

You could try Page::path or maybe one of the other page hooks. Don't forget Tracy's Captain Hook panel for easily finding hooks for a particular PW variable / object / class.

image.thumb.png.a1d53f75964ccd810c4b44a112e9c42b.png

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Ouah... I'm again discovering things here 🙂

@BitPoet   I've tested your code and here's what I get (Now what it means is a little over my head...) :

Capture du 2019-03-13 18-24-38.png

Capture du 2019-03-13 18-25-08.png

Share this post


Link to post
Share on other sites

The hooks in that list look good to me. A bit of a shot in the dark, but you might try and disable frontend editing in case it is enabled and see if that changes anything.

Share this post


Link to post
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 😞 

Share this post


Link to post
Share on other sites

I think you need to try disabling all autoload modules - Tracy's Module Disabler panel make this easy.

If that still doesn't work, then try a fresh PW install just to rule out anything else.

Share this post


Link to post
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 😉 

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
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 !

Share this post


Link to post
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).

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Glad it all worked out - we've all done similar things 🙂

  • Like 2
  • Thanks 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Macaco
      It's a bilingual site. There are two pages: "Artists" and "Events" each with a "Page Reference" field connecting each other.
      - Artists has a field where one can choose events available.
      - Events has a field where you can either choose artists available or create new ones.
      The problems: 
      - When I create an "Artist" page and select events from the list, it doesn't update the collection of participating artists on the "Event" page.
      - When I create an artist from the "Event" page. The field 'artist page > settings > language' is not "Active" for the second language.  When the artist page is created manually,"Active" is on by default.
      I know this all have to do with hooks, but I'm don't fully understand the logics.
    • By VeiJari
      Hello forum! 
      I started to write my first hook for Processwire but I'm pretty confused how you should write these. My idea is to hook after publishing in init.php (called before templates)  for a certain template. 
       
      Here's the code: 

      Trying to reset the checkbox (ajasta) and then saving it so it shows unchecked in the admin page when publishing "article" page. 
      But the code isn't doing anything. Not even dumping anything
      What seems to be the problem? And have you made a similar hook for this usage or am I doing it totally wrong? 😄
      Thanks for the support in advance!
    • By NorbertH
      Is there a hook to do something right after cloning a page ?
      I want the page to be saved right after cloning it either from the button in the tree or from a lister, because saving the page triggers several calculations that are not triggered by just cloning the page.
       
      Thanks in advance !
    • By tarkvsg
      Hi!
      tell me, pls, how to execute this equality (from ready.php)
      $wire->addHookAfter('InputfieldPage::getSelectablePages', function($event) {     if($event->object->hasField == 'FIELDNAME')     {         .....     } } - $hasField The Field object associated with this Inputfield (from definition parent class for InputfieldPage)
      the left side equation is the object entity, and the right side - the string 
      How it is?
    • By horst
      Hey,
      I want to hook into the 404 handling of PW at the most earliest stage. Pleas can anybody point me to the hook or file that suits this best?
×
×
  • Create New...