Drupal - How do I programmatically install and enable a module?
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"]);'