You are not logged in.

Announcement

 Téléchargez la dernière version stable de GLPI      -     Et vous, que pouvez vous faire pour le projet GLPI ? :  Contribuer
 Download last stable version of GLPI                      -     What can you do for GLPI ? :  Contribute

#1 2011-09-27 17:16:23

Reini
Member
Registered: 2009-10-21
Posts: 35

Question on function doHook

Hello everybody,
I ran into some trouble with a hook function that I created for the "pre_item_add_" hook. I created a plug-in for which I defined a pre_item_add hook. When testing the functionality of my hook, I realised that it was not called @ all. I had a look into the GLPI source code and found this piece of code in the function "doHook" (in file "inc/plugin.function.php"):

// Apply hook only for the item
   if ($param != NULL && is_object($param)) {
      $itemtype=get_class($param);
      if (isset($PLUGIN_HOOKS[$name]) && is_array($PLUGIN_HOOKS[$name])) {
         foreach ($PLUGIN_HOOKS[$name] as $plug => $tab) {
            if (isset($tab[$itemtype])) {
               if (file_exists(GLPI_ROOT . "/plugins/$plug/hook.php")) {
                  include_once(GLPI_ROOT . "/plugins/$plug/hook.php");
               }
               if (is_callable($tab[$itemtype])) {
                  call_user_func($tab[$itemtype],$data);
               }
            }
         }
      }
   } else { // Standard hook call
      if (isset($PLUGIN_HOOKS[$name]) && is_array($PLUGIN_HOOKS[$name])) {
         foreach ($PLUGIN_HOOKS[$name] as $plug => $function) {
            if (file_exists(GLPI_ROOT . "/plugins/$plug/hook.php")) {
               include_once(GLPI_ROOT . "/plugins/$plug/hook.php");
            }
            if (is_callable($function)) {
               call_user_func($function,$data);
            }
         }
      }
   }

Why is there a different handling for "items"? It seems that this is responsible for my hook function not to work. Why is there the call of the "is_callable" and the "call_user_func" functions with the parameter "$tab[$itemtype]" instead of the parameter "$function" like for other hooks?

And what do I have to do to get my "pre_item_add_" hook function into work?

Thanks in advance for your help.

Offline

#2 2011-09-27 17:19:11

remi
GLPI-DEV
From: Champagne
Registered: 2007-04-28
Posts: 7,127
Website

Re: Question on function doHook

How do you declare your hook ?

Must be something like

   $PLUGIN_HOOKS['pre_item_add']['example'] = array('Computer' => array('PluginExampleExample', 'pre_item_add_example'));

Dév. Fedora 29 - PHP 5.6/7.0/7.1/7.2/7.3/7.4 - MariaDB 10.3 - GLPI master
Certifié ITILv3 - RPM pour Fedora, RHEL et CentOS sur https://blog.remirepo.net/

Offline

#3 2011-09-27 17:25:20

Reini
Member
Registered: 2009-10-21
Posts: 35

Re: Question on function doHook

Wow, thanks for the super-fast answer.  smile

Oh no, I declared it like this:

$PLUGIN_HOOKS['pre_item_add']['mySuperPlugin'] = 'plugin_pre_item_add_mysuperplugin'; 

    wink

The plugin is used for tickets, must it be like this then:

$PLUGIN_HOOKS['pre_item_add']['mySuperPlugin'] = array('Ticket' => array('PluginExampleExample', 'plugin_pre_item_add_mysuperplugin'));

?

What do I have to use where you put 'PluginExampleExample' ?

Offline

#4 2011-09-27 18:02:13

remi
GLPI-DEV
From: Champagne
Registered: 2007-04-28
Posts: 7,127
Website

Re: Question on function doHook

The old syntaxe you are using (1 hook for all items is deprecated)

So for Ticket, you must use

$PLUGIN_HOOKS['pre_item_add']['mySuperPlugin'] = array('Ticket => 'plugin_pre_item_add_mysuperplugin'); 

If the plugin_pre_item_add_mysuperplugin exists in the hook.php file.

A better (more modern) solution is to use a method in one of the classes of your plugin, like the example I have paste from example plugin (method pre_item_add_example of PluginExampleExample class)


Dév. Fedora 29 - PHP 5.6/7.0/7.1/7.2/7.3/7.4 - MariaDB 10.3 - GLPI master
Certifié ITILv3 - RPM pour Fedora, RHEL et CentOS sur https://blog.remirepo.net/

Offline

#5 2011-09-28 08:40:02

Reini
Member
Registered: 2009-10-21
Posts: 35

Re: Question on function doHook

Thank you once again, Remi.

If I do use the "more modern" solution, is there a special file where I have to put the classes of my plugin? Also "hook.php"?

Offline

Board footer

Powered by FluxBB