Jump to content

Hooks inside hooks possible?


Gadgetto
 Share

Recommended Posts

-- Update -- The hooks in my module work! Some things to pay attention to:

  • Tracy's bd() call will maybe not be the best option for debugging, because you might have some session redirects going an and that might wipe your dump. So it could be possible that the hook is actually triggered but you will just not see the result.
  • First, I tried using $this->log('...') and saw some more output than using bd(), but the best option (another new learning using TracyDebugger) is this:
  • Using the log() funktion of Tracy is great:
    G9DmtlT.png

This is my setup:

// in site/init.php
$this->addHookBefore("RockCI::restoreRemote", function($event) {
  l('restore hooked in site/init.php');
});

// in site/ready.php
$this->addHookBefore("RockCI::restoreRemote", function($event) {
  l('restore hooked in site/ready.php');
});

// in RockCI module
$this->addHookBefore("restoreRemote", function($event) {
  l('restore hooked in RockCI module');
});

So everything here works as expected. Note that the hook in site/ready.php does never get triggered! I'm not 100% sure WHY, and that's why I wish something like this would exist: https://processwire.com/talk/topic/10857-making-hooks-visible-hookrecorder/

Also note that I'm using addHookBefore as the $session->redirect() might prevent any execution of hooks added after that method!

Does that help you?

Link to comment
Share on other sites

5 hours ago, bernhard said:

So everything here works as expected. Note that the hook in site/ready.php does never get triggered!

Good hint with Tracy log()!

But I don't understand what you tried to say: You say everything works like expected but then the hook in ready() isn't triggered. So you are having the same problem as I do?

Link to comment
Share on other sites

It's getting even more confusing - now it works even in /site/ready.php ?

iUofgF6.png

<?php namespace ProcessWire;
class RockHook extends WireData implements Module {

  public static function getModuleInfo() {
    return [
      'title' => 'RockHook',
      'version' => '0.0.1',
      'summary' => 'RockHook',
      'autoload' => true,
      'singular' => true,
      'icon' => 'bolt',
      'requires' => [],
      'installs' => [],
    ];
  }

  public function init() {
    bd('init()');
    bd(1);
    bd(2);
    bd(3);

    bd('add handleWebhook');
    $this->addHookAfter("ProcessPageView::execute", $this, "handleWebhook");

    bd('add hello-hook in init()');
    $this->addHookAfter('hello', function(HookEvent $event) {
      bd('hello-hook fired in init()');
    });
  }

  public function ready() {
    bd('ready()');
    
    bd('add hello-hook in ready()');
    $this->addHookAfter('hello', function(HookEvent $event) {
      bd('hello-hook fired in ready()');
    });
  }

  public function handleWebhook($event) {
    bd('handlewebhook fired');
    bd('trigger $this->hello()');
    $this->hello("trigger hello() in execute() hook");
  }

  public function ___hello($where) {
    bd('hello!', $where);
  }
}

 

  • Like 1
Link to comment
Share on other sites

  • 3 weeks later...
On 3/20/2020 at 3:47 AM, bernhard said:

Tracy's bd() call will maybe not be the best option for debugging, because you might have some session redirects going an and that might wipe your dump.

That's when it's time to turn on the Dumps Recorder panel ?

  • Like 2
  • Haha 1
Link to comment
Share on other sites

10 hours ago, adrian said:

That's when it's time to turn on the Dumps Recorder panel ?

This is great, didn't even know this exists!

I always have problems with bd() within modals or panels and when a redirect is involved!

Link to comment
Share on other sites

4 hours ago, Gadgetto said:

I always have problems with bd() within modals or panels

Do you have Tracy enabled for inside modals, panels, iframes? There is a config setting for disabling it in those to make things cleaner, but in most cases you probably want them on.

Link to comment
Share on other sites

1 hour ago, adrian said:

Do you have Tracy enabled for inside modals, panels, iframes? There is a config setting for disabling it in those to make things cleaner, but in most cases you probably want them on.

Yep, is enabled! On smaller displays (my MacBook) the 2 bars are overlapping sometimes, but it's great to have this possibility!

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