17

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?

27

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

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);
}
1

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
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.