Jump to content

Replace page render method and use original method


---
 Share

Recommended Posts

I've written a hook which replaces the page::render method. Based on a few conditions, I might want to output something different from what page::render would return.

So when my conditions aren't met, I want to use the return value of the original page::render method.

How can I do this?

At the moment I've got something like this:

function pageRenderHookMethod (HookEvent $event)
{
    if ('my condition' == true) {
        $event->replace = true;
        $event->return = 'my custom return value';
    } else {
        $event->return = 'original return value of page::render';
    }
}

But I don't know how to get the original return value of page::render because the would trigger my hooked method instead of the original method.

Link to comment
Share on other sites

function pageRenderHookMethod (HookEvent $event)
{
    $default = $event->return;
	if ('my condition' == true) {
        $event->return = 'my custom return value';
    } else {
        $event->return = $default;
    }
}


If you hook after you don't need $event->replace = true;

  • Like 1
Link to comment
Share on other sites

You don't even need to set the return value to it's previous value. Just don't modify it if you don't need to:

function pageRenderHookMethod (HookEvent $event)
{
    if ('my condition' != true) return;
    $event->return = 'my custom return value';
}

But most often it's the better solution to use a before hook and prevent the original render function from running uselessly if it's return value might be replaced anyways:

function pageBeforeRenderHookMethod (HookEvent $event)
{
    if ('my condition' != true) return; // Let the original $page->render() do it's job
    $event->replace = true; // Skip the original $page->render()
    $event->return = 'custom value';
}

 

  • Like 2
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...