Last active
April 20, 2017 17:22
-
-
Save litzinger/48f8743b6885675f8af3a122cd7c9d33 to your computer and use it in GitHub Desktop.
ExpressionEngine extension and last_call usage. https://docs.expressionengine.com/latest/development/extensions.html#multiple-extensions-same-hook
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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:
but unnecessary to check for template_fetch_template, because it is sending out data but not changing: