Wire::addHookAfter() method

Add a hook to be executed after the hooked method

  • Use an "after" hook when you have code that should execute after a hookable method executes.
  • One benefit of using an "after" hook is that you can have it modify the return value.

Example

// Attach hook to a method in current object
$this->addHookAfter('Page::path', $this, 'yourHookMethodName');

// Attach hook to an inline function
$this->addHookAfter('Page::path', function($event) { ... });

// Attach hook to a procedural function
$this->addHookAfter('Page::path', 'your_function_name');

// Attach hook from single object instance ($page) to inline function
$page->addHookAfter('path', function($event) { ... });

Usage

// basic usage
$string = $wire->addHookAfter($method, $toObject);

// usage with all arguments
$string = $wire->addHookAfter($method, $toObject, $toMethod = null, array $options = []);

Arguments

NameType(s)Description
methodstring, array

Method to hook in one of the following formats (please omit 3 leading underscores):

  • Class::method - If hooking to all object instances of the class.
  • method - If hooking to a single object instance.
  • Since 3.0.137 it may also be multiple methods to hook in CSV string or array.
toObjectobject, null, callable

Specify one of the following:

  • Object instance to call $toMethod from (like $this).
  • Inline function (closure) if providing implemention inline.
  • Procedural function name, if hook is implemented by a procedural function.
  • Null if you want to use the 3rd argument and don't need this argument.
toMethod (optional)string, array

Method from $toObject, or function name to call on a hook event. This argument can be sustituted as the 2nd argument when the 2nd argument isn't needed, or it can be the $options argument.

options (optional)array

Array of options that can modify behavior:

  • type (string): May be either 'method' or 'property'. If property, then it will respond to $obj->property rather than $obj->method(). The default type is 'method'.
  • priority (int): A number determining the priority of a hook, where lower numbers are executed before higher numbers. The default priority is 100.

Return value

string

A special Hook ID (or CSV string of hook IDs) that should be retained if you need to remove the hook later.


Wire methods and properties

API reference based on ProcessWire core version 3.0.236

Latest news

  • ProcessWire Weekly #549
    In the 549th issue of ProcessWire Weekly we’re going to check out the latest core updates, highlight one older yet still very relevant third party module, and more. Read on!
    Weekly.pw / 17 November 2024
  • Custom Fields Module
    This week we look at a new ProFields module named Custom Fields. This module provides a way to rapidly build out ProcessWire fields that contain any number of subfields/properties within them.
    Blog / 30 August 2024
  • Subscribe to weekly ProcessWire news

“To Drupal, or to ProcessWire? The million dollar choice. We decided to make an early switch to PW. And in retrospect, ProcessWire was probably the best decision we made. Thanks are due to ProcessWire and the amazing system and set of modules that are in place.” —Unni Krishnan, Founder of PigtailPundits