How to enable a module via code - Drupal 8/9
There are very rare occasions where you might need to enable modules via a hook_update instead of through a config import.
In order to do this, you would have to create a `.install` file within your custom module and use the hook_update_N hook.
For example, the following code will enable all the modules when `drush updb` is executed.
/**
* Enable a few modules
*/
function MY_MODULE_NAME_update_9001() {
$modules = ['webform', 'config_ignore', 'devel'];
foreach ($modules as $module) {
// we want to make sure these modules are not already enabled.
if (!\Drupal::moduleHandler()->moduleExists($module)) {
\Drupal::service('module_installer')->install([$module]);
}
}
}