0

I am developing a custom module, from this module im trying to disable other modules (for certain reasons).

Let's say I want to disable the search module, how would I do this?

2 Answers 2

1

You can actually prevent a module is enabled, implementing hook_system_info_alter() with code similar to the following one.

function mymodule_system_info_alter(&$info, $file, $type) {
  if ($file == 'search' && $type == 'module') {
    $info['dependencies'] = array('module_that_does_not_exist');
  }
}

Then, your module should use module_disable(array('search')) to disable the module.

In the case you need to dynamically disable modules basing on some conditions, you can verify those conditions in hook_system_info_alter(), but the modules must be disabled in another hook. The reason is that hook_system_info_alter() is (indirectly) called from system_rebuild_module_data(), and module_disable() calls system_rebuild_module_data(); that would cause a loop (system_rebuild_module_data() => hook_system_info_alter() => module_disable() => system_rebuild_module_data()).

hook_system_info_alter() is called for every module Drupal found.

 

As alternative, as Drupal 7 invokes hook_modules_enabled() when new modules are enabled, you can use the following code.

function mymodule_modules_enabled($modules) {
  if (in_array('search', $modules)) {
    drupal_set_message(t('mymodule is not compatible with the Search module'), 'error');
    module_disable(array('search'));
  }
}

Also in this case, if you need to dynamically disable modules basing on some conditions, you can verify those conditions are met in hook_modules_enabled().

2
  • I got this working, but this is not exactly what I want. I want to prevent the module from being loaded, it should not become an disabled module. Im making a cookie module, when the user doesnt accept cookies, certain modules should become 'hidden' or unusable modules. So this is different per user. Is there a 'unload' function or something?
    – mhmdrupal
    Commented Jul 2, 2012 at 8:11
  • There isn't an unload function, as all Drupal does when it loads a module is to use include_once(). See my updated answer.
    – avpaderno
    Commented Jul 2, 2012 at 9:23
1

See module_disable and the comments therein for how to place it within a profile.install or module.install file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.