Jump to content

Custom hooks in own Module


Recommended Posts

Hi there. I wrote a custom module for one of my projects. In fact I maybe want to use my module in other projects too. In order to be variable and customizable  I need to implement some custom hooks into my module. So I can afterwards hook into the my functions in order to modify them to match the needs of the new project.

I tried simply defining functions with the '__' prefix. But that did not work. I'm imagining something like the following:

<?php

class MyClass {
  public function ___someFunction() {
    // Do something
  }
}

// ready.php

$this->addHookBefore('MyClass::someFunction', function($event) {
	// some customization
});

Is there a way to accomplish that? 

Link to comment
Share on other sites

My bad, I figured it out. In order to work the class only needs to extend WireData. So the following example works.

<?php

class MyClass extends WireData {
  public function ___someFunction() {
    // Do something
  }
}

// ready.php

$this->addHookBefore('MyClass::someFunction', function($event) {
	// some customization
});

 

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