Skip to content

Instantly share code, notes, and snippets.

@litzinger
Last active April 20, 2017 17:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save litzinger/48f8743b6885675f8af3a122cd7c9d33 to your computer and use it in GitHub Desktop.
Save litzinger/48f8743b6885675f8af3a122cd7c9d33 to your computer and use it in GitHub Desktop.
<?php
/**
* @var mixed $data
* This is the incoming parameter value. If multiple add-ons use the same hook the same $data value
* is given to each hook call, regardless of what order the hooks are called in.
*/
public function some_extension_hook($data)
{
// If last_call contains a value, then it means another extension used this same hook before yours,
// so use the modified data from that hook, not the value of $data from the function parameter.
// Now you have the most recent data to work with. If you don't do this and simply return the
// original value of $data from the function parameter you're basically nullifying the hard work
// the add-on developer did in the extension hook called before yours and creating an add-on conflict.
// ** Even if you don't need to modify $data, add this conditional. **
if (ee()->extensions->last_call) {
$data = ee()->extensions->last_call;
}
// Do something with $data, then return your modified version for the next add-on. Pay it forward.
return $data;
}
@aaronwaldon
Copy link

aaronwaldon commented Apr 20, 2017

This is often true, but not always. EE has several hooks where the value is read-only and cannot be changed. In those cases, there is no need to see if the value has been modified (because it cannot be).

For example, it would be necessary to check for template_post_parse, because it updates a value based on the results from the call:

$this->final_template = ee()->extensions->call(
    'template_post_parse',
    $this->final_template,
    $is_partial,
    $site_id
);

but unnecessary to check for template_fetch_template, because it is sending out data but not changing:

ee()->extensions->call('template_fetch_template', $row);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment