4

For Drupal 7, I found How do I programmatically install and enable a module?

What is the equivalent code for Drupal 8?

UPD: here Configuration Manager or hook_update() to enable modules I found the case when you can use the code during deployments, specially when your module has updates in own dependencies.

1

1 Answer 1

8

Use "module_installer" service.

$installer = \Drupal::service('module_installer');
// Or from the container.
$installer = $container->get('module_installer');

// Install single module.
$installer->install(['module_name']);
// Install multiple modules.
$installer->install(['module_name_1','module_name_2']);

Also for uninstallation of the module use the next example:

$installer->uninstall(['module_name']);

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