Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

standard hook plugin_x_deactivate and post_plugin_disable not called on plugin deactivate #16075

Closed
2 tasks done
flex-pgum opened this issue Nov 28, 2023 · 2 comments
Closed
2 tasks done
Labels
Milestone

Comments

@flex-pgum
Copy link

Code of Conduct

  • I agree to follow this project's Code of Conduct

Is there an existing issue for this?

  • I have searched the existing issues

Version

10.0.10

Bug description

Both "plugin_x_deactivate" (standard hook in hook.php) and "post_plugin_disable" are not called when disabling any custom plugin.

Relevant log output

No response

Page URL

No response

Steps To reproduce

  1. Add a small custom module (maybe the https://github.com/pluginsGLPI/example plugin)
  2. Add function plugin_example_deactivate to hook.php with an error_log or something similar to see if function is called (in case you installed and configured xdebug, simply set a breakpoint)
  3. Configure the post_plugin_disable hook in setup.php, create the function in hook.php and set a breakpoint (or log)
  4. Install and activate the plugin
  5. Deactivate the plugin

Your GLPI setup information

No response

Anything else?

I did some debugging and found the reason for both issues:

  • plugin_x_deactivate: in file Plugin.php (./src/Plugin.php) under function unactivate the respective hook.php is not included like in for example activate
  • post_plugin_disable: in file Plugin.php (./src/Plugin.php) under function doHook there is a isPluginActive condition which prevents the post_plugin_disable hook to execute. This is a logical error because the plugin is and can not be active anymore when the post_plugin_disable hook is called.

I would be glad if anyone with some more glpi-knowledge could look into this and confirm my thoughts.

@cedric-anne
Copy link
Member

The following patch should fix the call to plugin_x_deactivate hook.

diff --git a/src/Plugin.php b/src/Plugin.php
index 79fd2a15d0..465343e0b4 100644
--- a/src/Plugin.php
+++ b/src/Plugin.php
@@ -1116,6 +1116,9 @@ class Plugin extends CommonDBTM
     {
 
         if ($this->getFromDB($ID)) {
+            // Load plugin hooks
+            self::load($this->fields['directory'], true);
+
             $deactivate_function = 'plugin_' . $this->fields['directory'] . '_deactivate';
             if (function_exists($deactivate_function)) {
                 $deactivate_function();

For the post_plugin_disable hook, it will not be call for the plugin that has just been disabled, and it is normal. The purpose of this hook is to indicate to other plugins that your plugin has been deactivated.

@cedric-anne cedric-anne added this to the 10.0.11 milestone Nov 28, 2023
@flex-pgum
Copy link
Author

The following patch should fix the call to plugin_x_deactivate hook.

diff --git a/src/Plugin.php b/src/Plugin.php
index 79fd2a15d0..465343e0b4 100644
--- a/src/Plugin.php
+++ b/src/Plugin.php
@@ -1116,6 +1116,9 @@ class Plugin extends CommonDBTM
     {
 
         if ($this->getFromDB($ID)) {
+            // Load plugin hooks
+            self::load($this->fields['directory'], true);
+
             $deactivate_function = 'plugin_' . $this->fields['directory'] . '_deactivate';
             if (function_exists($deactivate_function)) {
                 $deactivate_function();

For the post_plugin_disable hook, it will not be call for the plugin that has just been disabled, and it is normal. The purpose of this hook is to indicate to other plugins that your plugin has been deactivated.

Nice, wasn't sure if the load function is just enough. Thank you very much.
For the post_plugin_disable hook: It seems a little bit strange but okay, normally I would have assumed that I can define a function that is executed when this hook is called like on any other hook.

cedric-anne added a commit to cedric-anne/glpi that referenced this issue Dec 6, 2023
anthonymontebrun pushed a commit to IT-Gouvernance/glpi that referenced this issue Jul 11, 2024
btry pushed a commit to btry/glpi that referenced this issue Sep 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants