Jump to content
Sign in to follow this  
schwarzdesign

Page::render hook has no arguments

Recommended Posts

I just spent some time trying to get a hook in a site/init.php file to work. What I wanted to do was disable the template/page cache based on the value of some field of the current page. I tried following @Soma's example here:

Unfortunately, this doesn't work at all for me, since the hook gets called with no arguments. I tried this:

wire()->addHookBefore('Page::render', function (HookEvent $event) {
    bd($event->arguments(0));
    bd($event->arguments(1));
});

But both just evaluate to null. So I dug deep to find the PageRender::renderPage method and tried to go through there. I finally got it to work using this rather obscure syntax:

wire()->addHookBefore('PageRender::renderPage', function (HookEvent $event) {
    $page = $event->arguments(0)->object;
    $disableCache = $page && $page->hasField('cta_display');
	if ($isValidPage) {
		$event->arguments(0)->setArgument(0, ['allowCache' => false]);
    }
});

I'd like to know why the Page::render hook doesn't receive any arguments? Was there some change regarding this since Soma's post? Since the version linked above doesn't work at all for me. What would be the 'correct' way to accomplish this? I'd appreciate any insights. Thanks!

ProcessWire Version: 3.0.130
PHP Version: 7.2

Share this post


Link to post
Share on other sites

no argumentas needeed 
what.u wants is event->return

wire()->addHookAfter('Page::render', function (HookEvent $event) {
    $page = $event->return;
});

  • Like 4
  • Thanks 1

Share this post


Link to post
Share on other sites
12 hours ago, WillyC said:

no argumentas needeed 
what.u wants is event->return

wire()->addHookAfter('Page::render', function (HookEvent $event) {
    $page = $event->return;
});

Thanks, but I don't think hooking after the page render will work, since I want to conditionally disable the render cache, so at this point it's too late ..

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.

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...