How do I programmatically install a module? [duplicate]
-
29-01-2021 - |
Question
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.
Solution
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']);
Licensed under: CC-BY-SA with attribution
Not affiliated with drupal.stackexchange