Group icon
A group for those interested in plugin development

Pages home > Engine reference > Plugin hooks

Plugin hooks

Plugin hooks are similar to Events, except that it can be passed a parameter array and a return value which (which will be the default value returned if one of the hook listeners overrides it).

Registering a hook

To register a hook, use the function 'register_plugin_hook', eg.

Parameters

  • $hook The name of the hook.
  • $entity_type The name of the type of entity (eg "user", "object" etc) or 'all'.
  • $function The name of a valid function to be run.
  • $priority The priority - 0 is first, 1000 last, default is 500.
register_plugin_hook("import", "all", "import_entity_plugin_hook", 0);

Your function hook

Your function hook should have the following prototype:

function my_plugin_hook($hook, $entity_type, $returnvalue, $params)
{
 
... do stuff ...
 
}

Parameters

  • $hook The hook being called.
  • $entity_type The type of entity you're being called on.
  • $returnvalue The return value. IMPORTANT: Unless you are adding to or otherwise changing the return value DO NOT RETURN ANYTHING.
  • $params An array of parameters.

,

Last updated 532 days ago by Pete Harris

Maybe I'm missing something, but this doesn't really explain much, like when I should use what entity type, or how register_plugin_hook and my_plugin_hook relate to each other.

horace 514 days ago

I get the whole register & my_plugin_hook, but what I can't figure out is how to add to the return value.  I've tried defining my_plugin_hook (...., &$returnvalue ....) which I assumed would allow me to add a value to the return value array, but I was wrong.

How do I add/affect the return value?

Chris 344 days ago

Thanks.  Yes,  array was clear, but the behaviour wasn't -- but for others who are interested (and for a check of my understanding, please correct me if I got it wrong), I finally concluded as follows:

  ** The argument $returnvalue might better be called $previous_returnvalues_for_hook; in other words, it is actually the value returned by the previous function executed for the hook, not necessarilly anything to do wit the value you return.

  ** Your function then has 3 different return possibilities:

    1) Return nothing.  In which case he return value of the trigger will be whatever the previous return value was in the arg $returnvalue

    2) Over-ride the previous return value; like:  return "Whatever";  Then the string "Whatever" will be returned, the previous return values will be discarded.

    3) Add your return value to the previous return values; in which case you might say:

          $returnvalue["mypluginhook"]="Whatever";

          return $returnvalue;

 

In which case the return value of your current function will be added to whatever value was returned by the previous function (based on the priority number) triggered in this hook.

 

Is that about right, or am I way off?

Chris 344 days ago

ohhh lawdy !

all i said was one word    'n you give me semester thesis ;-O)

good to know yew got it working ;;)

i see you did your code stuff study rather well.

DhrupDeScoop 344 days ago