20

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?

4 Answers 4

30

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);
2
  • Does this enable the modules in the same order as they are in the array? Commented May 16, 2018 at 15:25
  • 1
    No @BrentConnor, a dependency map is computed and they're installed in whatever order make sense
    – Clive
    Commented May 16, 2018 at 15:35
12

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);
}
0
4

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"]);'
1
  • If you want IDE introspection, use this: ``` /** @var \Drupal\Core\Extension\ModuleInstallerInterface $moduleInstaller */ $moduleInstaller = \Drupal::service("module_installer"); $moduleInstaller->install(['readmehelp']); ``` Commented Dec 3, 2019 at 11:25
2

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

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.