The new version of my module depends on more modules than the old version … how to ensure module users install them?
-
16-10-2019 - |
Pergunta
I am updating an existing module I have developed. The new version requires a "helper" module that the old version did not (fwiw, specifically "Nodereference Count"). How do I ensure that when people upgrade from the prior version of my module to the new version, they make sure to install the newly required helper module first?
Is there something I should put into hook_update_n function?
Thanks.
Solução
You could use hook_requirements(), which should allow you to display an error when the module is installed/updated.
Alternatively, if that doesn't work you can implement hook_init() to run a check for the modules and react accordingly.
Update: I've just run into the same issue myself and here is the code that I have used:
/**
* Implements hook_init().
*/
function MYMODULE_init() {
if (!module_exists('ctools')) {
module_enable(array('ctools'));
if (!module_exists('ctools')) {
drupal_set_message(t('MYMODULE has been disabled as the required !ctools module is not present.', array('!ctools' => l(t('Chaos tool suite'), 'http://drupal.org/project/ctools'))), 'error');
module_disable(array('MYMODULE'));
}
}
}
This will try to enable the required module and if it is unable to do so it will display an error message to the user and disable the my module.