There is a lot resource for doing it in Drupal 6, but I didn't find a solution for Drupal 7.
How can I install and enable selected modules?
There's just one step, using module_enable()
:
$modules = array('module1', 'module2'); // Array of module names
$enable_dependencies = TRUE; // Whether or not to enable dependant modules
module_enable($modules, $enable_dependencies);
Here is how you would do it with a database update using hook_update_N
from another enabled module's *.install
file. Then you can visit /update.php
in the browser or run $ drush updb
on the command line to have this code fired.
/**
* Enable module1 and module2.
*/
function MYMODULE_update_7101() {
// Array of module names.
$modules = ['module1', 'module2'];
// Whether or not to enable dependant modules.
$enable_dependencies = TRUE;
module_enable($modules, $enable_dependencies);
}
For drupal 8 you can use the following:
\Drupal::service("module_installer")->install(["my_custom_module"]);
In drush:
drush php-eval '\Drupal::service("module_installer")->install(["my_custom_module"]);'
In Drupal 7 there is no more drupal_install_modules(), therefore if you want to force the module installation process programmatically (if it's already enabled), you can use:
module_invoke('module_name', 'install');
To enable, it's simply:
module_enable(array('module_name'));
Or using drush
as part of the deployment process:
drush -y en module_name