We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Provide own hooks

Development and Programming

mweichert's avatar
mweichert
68 posts
about 17 years ago
mweichert's avatar mweichert

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

       
Justin Hurlburt's avatar
Justin Hurlburt
344 posts
about 17 years ago
Justin Hurlburt's avatar Justin Hurlburt

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);
}
       
gridonic's avatar
gridonic
231 posts
about 17 years ago
gridonic's avatar gridonic

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;
}
       
mweichert's avatar
mweichert
68 posts
about 17 years ago
mweichert's avatar mweichert

Thanks guys! I’ve got it.

       
Jeff Ricks's avatar
Jeff Ricks
99 posts
16 years ago
Jeff Ricks's avatar Jeff Ricks
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.

       
Jeff Ricks's avatar
Jeff Ricks
99 posts
16 years ago
Jeff Ricks's avatar Jeff Ricks

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.

       
Jeff Ricks's avatar
Jeff Ricks
99 posts
16 years ago
Jeff Ricks's avatar Jeff Ricks

Anyone? Isn’t there a discrepancy as decribed in my previous post? I need to use that hook but it’s not working for me, so I need to know if the discrepancy is the problem.

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.