Hello,
In my plugin, I want to provide a hook that extensions can implement. For example, one of the hooks is the ability to rewrite the output generated by the plugin.
If I have a simple plugin like this:
class MyPlugin
{
function say_hello()
{
$out = "Hello";
return $out;
}
}
I would think to add a custom hook, I’d change the say_hello function to look like this:
function say_hello()
{
global $EXT;
$out = "Hello";
$out = $EXT->call_extension('my_custom_hook', $out);
return $out;
}
What happens though, is that
$EXT->call_extension('my_custom_hook', $out);
doesn’t return anything if no extension has been developed to modify $out.
Looking through the source code of EE, I couldn’t really see how it is supposed to be done. Am I supposed to check if $out has been set, or am I supposed to pass $out by reference, or …?
Thanks for your help!
Cheers, Mike
Greetings Mike,
The way hooks work is that you subscribe to them. So what you would need to do is check for subscriptions before setting the data of a hook that isn’t being subscribed to.
if ($EXT->active_hook('my_hook') === TRUE)
{
$hook_data = $EXT->call_extension('my_hook', $data);
}
Have a look at the freeform module from solespace, they have a nice example of using your own hooks in their code.
/** ----------------------------------------
/** 'freeform_module_validate_end' hook.
/** ----------------------------------------
/* This allows developers to do more
/* form validation.
/** ----------------------------------------*/
if (isset($EXT->extensions['freeform_module_validate_end']))
{
$errors = $EXT->call_extension('freeform_module_validate_end', $errors);
if ($EXT->end_script === TRUE) return;
}
Greetings Mike, The way hooks work is that you subscribe to them. So what you would need to do is check for subscriptions before setting the data of a hook that isn’t being subscribed to.if ($EXT->active_hook('my_hook') === TRUE) { $hook_data = $EXT->call_extension('my_hook', $data); }
I need some help with understanding the above comment. I’m creating an extension and have succeeded in implementing code that uses two hooks. The third hook I would like to use tests if the hook is active (as in your example above), and the test fails, so my code for that hook isn’t executed either. Why do some hooks test for “active_hook” and others don’t. Also, please elaborate on how a hook is subscribed to. I’ve never heard of this before.
I’ve discovered what looks to me like a discrepancy between the EE hooks documentation and the actual code.
The documentation says the hook for ‘forum_submit_post_end’ looks like this:
if (isset($EXT->extensions['forum_submit_post_end']))
{
$edata = $EXT->call_extension('forum_submit_post_end', $this, $data);
if ($EXT->end_script === TRUE) return $edata;
}
But the actual code for that hook in mod.forum.core.php (version 2.1.1) looks like this. Notice the ‘if’ statement is different:
if ($EXT->active_hook('forum_submit_post_end') === TRUE)
{
$edata = $EXT->universal_call_extension('forum_submit_post_end', $this, $data);
if ($EXT->end_script === TRUE) return $edata;
}
I’m wondering if the discrepancy is the reason my call to that hook isn’t working, whereas my calls to other hooks are working just fine. Any help is appreciated.
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.