You are not logged in.
Pages: 1
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
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
Wow, thanks for the super-fast answer.
Oh no, I declared it like this:
$PLUGIN_HOOKS['pre_item_add']['mySuperPlugin'] = 'plugin_pre_item_add_mysuperplugin';
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
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
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
Pages: 1