How to enable/disable modules programmatically

When you add new modules to your project, you may be used to enabling them in the modules page (admin/modules). This does mean that you need to remember to enable these modules on the live site (and any other site in your pipeline).

Like most things in Drupal, you can automate the enabling of modules by doing it programmatically. This means that when you deploy your new modules to the new site, you won't need to manually enable them. This saves time and reduces the chance that you accidentally forget which modules to enable.

Enable in an install file

The most common method, and the one I use, is to enable modules in an install file.

You can use a single function called module_enable() to do this. This function takes an array of modules that you want to enable.

module_enable(array('module_1', 'module_2', module_3));

By default, dependencies of the modules being enabled will also be enabled. But if you decide that you don't want to enable dependencies, add FALSE as a second argument.

module_enable(array('module_1', 'module_2', module_3), FALSE);

Put this code in an update_N function in an install function. It is common to use a central deployment module to handle this. When you run update.php or drush updb, the module(s) will be enabled.

Here is a real world example of module_enable in a update_N function.

  1. function site_deploy_update_7001() {
  2. module_enable(array('entity_view_mode', 'maxlength'));
  3. }

Disable a module

If you want to disable a module, there is a conveniently named sister function called module_disable().

module_disable(array('module_1', 'module_2', module_3));

Dependencies will be automatically be disabled. Because this will result in a non-trivial performance hit, you can set this to FALSE if you know that you want to keep any dependent modules enabled.

module_disable(array('module_1', 'module_2', 'module_3')), FALSE);

Uninstall a module

You'd normally want to uninstall a module after disabling it and you need to call a second function for that, drupal_uninstall_modules().

module_disable(array('module_1', 'module_2', 'module_3'));
drupal_uninstall_modules(array('module_1', 'module_2', 'module_3'));

Let's take a look at another real world example in a custom deployment module. This will disable and uninstall the same modules that were enabled in the previous example.

  1. function site_deploy_update_7002() {
  2. module_disable(array('entity_view_mode', 'maxlength'));
  3. drupal_uninstall_modules(array('entity_view_mode', 'maxlength'));
  4. }

Further reading

Programmatically enabling and disabling modules is a good example of automating updates. If you want to learn more about setting up a development module, check out Automate Drupal site updates with a deployment module.

Comments

If doing this in update hooks, it's worth checking whether it worked, and throwing an error if it didn't. We use a helper function for this:

  1. /**
  2. * Enables modules and throws an exception if that can't be done.
  3. *
  4. * @param $modules
  5. * A string containing a module name or an array of module names.
  6. */
  7. function cm_tools_module_enable($modules) {
  8. if (is_string($modules)) {
  9. $modules = array($modules);
  10. }
  11.  
  12. $result = module_enable($modules);
  13.  
  14. if (empty($result)) {
  15. throw new DrupalUpdateException(implode(', ', $modules) . ' or dependencies could not be enabled');
  16. }
  17. }
Blair Wadman's picture

Nice helper function James - thanks for sharing it!

Hi, where I have to write the code for enable the module and how that enable_module function will call ?

Hi Nisha, you may create another module to perform that task; and you should implement code in it's .install file. For example, to enable module A, create a new module, B, and in B.install file implement a hook_update function that contains enable_module function's call.
And thanks Wadman for writing this article.

Add new comment