Jump to content
---

Replace page render method and use original method

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.

Share this post


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

Share this post


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

Share this post


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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By jploch
      Hi folks,
      i try to use a hook to change some markup inside admin, this hook works fine inside a ready.php file, but its not working inside my module:
      <?php namespace ProcessWire; class InputfieldPageTableExtendedGrid extends InputfieldPageTable { public static function getModuleInfo() { return array( 'title' => __('Inputfield for PageTableExtendedGrid', __FILE__), // Module Title 'summary' => __('Adds Inputfield for PageTableExtendedGrid', __FILE__), // Module Summary 'version' => 233, 'requires' => array('FieldtypePageTableExtendedGrid'), 'permanent' => false, ); } public function ready() { $this->addHookAfter('Page::render', function($event) { $value = $event->return; // Return Content $style = "<style type='text/css'>". $this->pages->get((int) wire('input')->get('id'))->style ."</style>"; // Add Style inside bottom head $event->return = str_replace("</head>", "\n\t$style</head>", $value); // Return All Changes }); } } whats wrong here?
    • By Sten
      Hello
      Till now I hacked something with the twig template but it works no more with new PW versions so I look forward to create a module. I am working on a site in multiple languages : French, English, Italian, German, Spanish, Portuguese, Hebrew, Russian. The new posts are entered in any language with a field for language. Till now, I got twig files to get the translations with constants defined for each part of the pages.
      So I'd like to create a module to include theses files added according to the url /fr/en/...
      Have you some observations to do before I begin about the direction to take ?
      Thank you
    • By EyeDentify
      Hello dear PW gurus.

      I have stumbled over a strange error that i all of sudden got when trying to upload an image to a images field on a page.
      There where images allready stored in the field that i wanted to keep, but during the upload the error apear and after that all images are gone from the field and i can´t upload any, i just get the error every time.
      I am running ProcessWire 3.0.153 dev.
      Update:
      After looking in the assets folder i find the folder for the page and the image files seems to be there including the ones i tried to upload when the error occured.
      But they don´t show up in the images field in the page editor.
       
      The error reported:
       
      SQLSTATE[01000]: Warning: 1265 Data truncated for column 'ratio' at row 1 And here is a screenshot of the event:

      The TracyDebugger Error reporting:

      I hope you fine folks could point me in a direction.
      But it seems our old pal set_time_limit() is back.

      Regards, EyeDentify
    • By Spyros
      Hello
      I'm having a strange issue with the $page->find(), for some reason I'm missing some of the pages from the results. I found then that I was missing all the pages with the same "PAGE NAME". Is it a bug or am I missing something?
      PS 
      If I change the "PAGE NAME" of one of the missing ones then I'm retrieving the page without any problem.
      Thank you
×
×
  • Create New...