Jump to content

Strange behaviour on hook after page render


Juergen
 Share

Recommended Posts

Hello

I have discovered a strange issue after running a hook after page render. So this is the hook that I run within the init() method inside a module:

public function init(){
	// add CSS and JS links to the frontend template
    $this->addHookAfter('Page::render', $this, 'addAssets');
}

protected function addAssets(HookEvent $event):void
    {
        $page = $event->object;
        // don't add this to the admin pages
        if ($page->template->name === 'admin') {
            return;
        }
        $version = $this->getModuleInfo()['version'];
        if (!$this->input_removeJS) {

            $js = '<script src="' . $this->wire('config')->urls->$this . 'frontendforms.js?v=' . $version . '"></script>' . PHP_EOL;
            $event->return = str_replace('</body>', $js . PHP_EOL . '</body>', $event->return);
        }
        if (!$this->input_removeCSS) {
            $css = '<link rel="stylesheet" type="text/css" href="' . $this->wire('config')->urls->$this . 'frontendforms.css' . '"/>' . PHP_EOL;
            $event->return = str_replace('</head>', $css . PHP_EOL . '</head>', $event->return);
        }
    }

This hook method adds a javascript script tag and CSS link tag to the page on the frontend. So far so good. It has worked til now until problems. Now I have added this code to a newly created module, but now it does not work in this case. Take a look what Tracy says:

 

hook.thumb.png.e33038b13bb44f490beab717d75b4e64.png

You can see the calls of 2 different modules using the same hook after page render code:  The first one is the new module and the second one is from my FrontendForms module. Just to keep in mind: both modules use the same hook and the same hook function to add script and CSS to the frontend.

As you can see, the first module returns "null", but should return a string. The second one (the FrontendForms module) returns a string as expected.

I cannot see any problems via Tracy, so I have no idea, why this happens. The only difference that I can see is on the id key:

'id' => 'Page:100.3:render'
'id' => 'Page:100.0:render'

Does anyone has an idea, what could be the cause, that the first hook returns null instead of a string????

Thanks in advance

Link to comment
Share on other sites

5 minutes ago, Juergen said:

As you can see, the first module returns "null", but should return a string.

The question is: is $event->return already null when your hook is entered? If yes, the problem happens earlier.

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

2 minutes ago, BitPoet said:

The question is: is $event->return already null when your hook is entered? If yes, the problem happens earlier.

I only can confirm that $event->return is null inside the addAssets() method, but I cannot find out, where it probably happens earlier. So the $event variable which will be entered as the parameter inside the brackets has null as return parameter.

Is there way to find out WHERE the problem could be?

 

Link to comment
Share on other sites

Thanks! This is an interesting approach, that I have never used before.

This is what I get so far:
 

0 =>array

'file' => 'C:\xampp\webseite1\wire\modules\Process\ProcessPageView.module:184'

'call' => '$page->render()'

1 =>array

'file' => 'C:\xampp\webseite1\wire\modules\Process\ProcessPageView.module:114'

'call' => 'ProcessPageView $process->renderPage(Page $obj, PagesRequest $obj)'

2 =>array

'file' => 'C:\xampp\webseite1\index.php:55'

'call' => 'ProcessPageView $process->execute([ true ])'

1) returns the string as expected -> OK

2) returns the string as expected -> OK

3) returns the string as expected -> OK

 

Unfortunately, every 3 calls return the HTML code as a string as expected, but thank you for this tip - I will keep this in mind for the future ?

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