Chock full o’Hooks
With its unique, powerful and easy-to-use hooks system, ProcessWire gives you the power and capability to accomplish any need that comes up.
Almost everything is hookable! ProcessWire contains more than a thousand hookable methods that enable you to modify the behavior of a called method.
$pages->addHook('saved', function(HookEvent $event) {
$page = $event->arguments(0);
$changes = implode(', ', $event->arguments(1));
$event->message("Saved page: $page->path - changes: $changes" );
});
You can hook before a method call to inspect or modify the arguments sent to the method (or if needed, to entirely replace the called method with your own).
$wire->addHookBefore('SomeClass::someMethod', function($event) {
$value = $event->arguments(0); // GET first argument
$event->arguments(0, 'hello'); // SET first argument
});
You can hook after a method in order to inspect or modify the value returned from it.
$wire->addHookAfter('SomeClass::someMethod', function($event) {
$value = $event->return; // get method return value
$event->return = 'hello world'; // set method return value
});
Hooks can also be used to add new methods or properties to existing classes.
$wire->addHookProperty('Page::full_name', function($event) {
$page = $event->object;
$event->return = "$page->first_name $page->last_name");
});
ProcessWire also supports URL/path hooks, which enable you to hook into any requested URL and control its output.
$wire->addHook('/hello/world', function($event) {
return 'Hello World';
});
It's not just ProcessWire methods that are hookable, but any class method that starts with 3 underscores becomes hookable in ProcessWire. Module developers often use this to make the methods in their module hookable, as easily as this:
class MyClass extends Wire {
public function ___hello($name) {
return "Hello $name";
}
}
Hooking that method and modifying its return value is as easy as this:
$wire->addHookAfter('MyClass::hello', function() {
$name = $event->arguments('name');
$event->return .= "Hi $name, how are you?";
});
Hooks are one of the more powerful and unique aspects of ProcessWire, and it's one of many reasons why ProcessWire is not just a CMS, but also a powerful and highly customizable content framework. Learn more about hooks