module_disable() contains the following code comment:

+    // Invoke hook_modules_disabled before disabling modules,
+    // so we can still call module hooks to get information.

However it doesn't follow that comment. Attached patch fixes that.

Comments

fago’s picture

StatusFileSize
new2.27 KB

added a test.

klausi’s picture

StatusFileSize
new1.36 KB

I think the test does not trigger the error. Here is patch that only changes the test case and it does not fail on my machine. Let's see what the bot says.

fago’s picture

StatusFileSize
new1.38 KB

indeed, module_hook() and so module_invoke() works regardless whether the module is enabled. The problem is though, module_invoke_all() uses module_implements() which does not work for disabled modules. That inconsistency is another issue though.

Attached patch contains only the test improved to also check module_implements(), thus it should fail.

Status: Needs review » Needs work

The last submitted patch, drupal_module_disable_test.patch, failed testing.

fago’s picture

Status: Needs work » Needs review
StatusFileSize
new2.28 KB

Ok, here is a patch including the improved test + the fix. Should be green now.

drunken monkey’s picture

Subscribe.

fago’s picture

Any reviews? This is required to make #978832: enable modules to apply configuration (exportable entities) actually work.

drunken monkey’s picture

Status: Needs review » Needs work

Tried it, but doesn't seem to work.

Setup:

function module1_modules_disabled($modules) {
  dsm('hook_modules_disabled()');
  module_invoke_all('foobarbaz');
}

function module2_foobarbaz() {
  dsm('hook_foobarbaz()');
}

When disabling module2, only the "hook_modules_disabled()" message is displayed.

klausi’s picture

Status: Needs work » Reviewed & tested by the community

As fago said: you cannot use module_invoke_all() as it uses module_implements() which does not work at this point (this is a separate issue).

Otherwise the test now correctly triggers the error, RTBC for me.

fago’s picture

ad #8: Try running the test-case of #3 with and without the fix applied, that should work.

drunken monkey’s picture

[Sorry, duplicate comment — had some weird display bug here just now.]

drunken monkey’s picture

Then what exactly does this patch fix?

Also, as mentioned in #978832: enable modules to apply configuration (exportable entities), your own code using this doesn't work for me, so there has to be a bug somewhere.

drunken monkey’s picture

Sorry, my bad — made a stupid mistake when testing.

Patch seems to be RTBC to me, too.

dries’s picture

Why do we want to move the code, versus updating the code comment.

Based on the name, I'd expect hook_modules_disabled() to be called after the modules have been disabled.

If the hook is going to be called before the modules are disabled, I'd expect it to be called hook_module_disable(), i.e. no trailing 'd'.

fago’s picture

Title: hook_module_disabled() implementations can't use module hooks » fix hook_module_disabled() comment and add tests
Status: Reviewed & tested by the community » Needs review
StatusFileSize
new2.04 KB

>Based on the name, I'd expect hook_modules_disabled() to be called after the modules have been disabled.

Ops, indeed, that's the only way it makes sense. Also the hook description says "Perform necessary actions after modules are disabled." - thus modules have to be disabled.

In my use-case #978832: enable modules to apply configuration (exportable entities) I'd need to be able to invoke the module's hooks in order to get the exportables defined by the modules being disabled. Thus having enabled modules would be useful, but well, that would be pretty inconsistent with the hook description.

So, I've re-rolled the patch to fix the comment instead + changed the test to make sure modules are actually disabled.

Status: Needs review » Closed (outdated)

Automatically closed because Drupal 7 security and bugfix support has ended as of 5 January 2025. If the issue verifiably applies to later versions, please reopen with details and update the version.