Jump to content

Page::render hook has no arguments


schwarzdesign
 Share

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

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

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