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?
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()
.
include_once()
. See my updated answer.
See module_disable and the comments therein for how to place it within a profile.install or module.install file.