Helper function to install a new module in Drupal 8 via hook_update_N().
function update_module_enable(array $modules) {
$schema_store = drupal_container()
->get('keyvalue')
->get('system.schema');
foreach ($modules as $module) {
// Check for initial schema and install it. The schema version of a newly
// installed module is always 0. Using 8000 here would be inconsistent
// since $module_update_8000() may involve a schema change, and we want
// to install the schema as it was before any updates were added.
$function = $module . '_schema_0';
if (function_exists($function)) {
$schema = $function();
foreach ($schema as $table => $spec) {
db_create_table($table, $spec);
}
}
// Enable the module with a weight of 0.
$module_config = config('system.module');
$module_config
->set("enabled.{$module}", 0)
->set('enabled', module_config_sort($module_config
->get('enabled')))
->save();
// Ensure the module is not contained in disabled modules.
config('system.module.disabled')
->clear($module)
->save();
// Change the schema version from SCHEMA_UNINSTALLED to 0, so any module
// updates since the module's inception are executed in a core upgrade.
$schema_store
->set($module, 0);
// system_list_reset() is in module.inc but that would only be available
// once the variable bootstrap is done.
require_once DRUPAL_ROOT . '/core/includes/module.inc';
system_list_reset();
// @todo: figure out what to do about hook_install() and hook_enable().
}
}