Enables or installs a given list of modules.
Definitions:
Order of events:
$module_list: An array of module names.
$enable_dependencies: If TRUE, dependencies will automatically be added and enabled in the correct order. This incurs a significant performance cost, so use FALSE if you know $module_list is already complete and in the correct order.
FALSE if one or more dependencies are missing, TRUE otherwise.
function module_enable($module_list, $enable_dependencies = TRUE) {
if ($enable_dependencies) {
// Get all module data so we can find dependencies and sort.
$module_data = system_rebuild_module_data();
// Create an associative array with weights as values.
$module_list = array_flip(array_values($module_list));
while (list($module) = each($module_list)) {
if (!isset($module_data[$module])) {
// This module is not found in the filesystem, abort.
return FALSE;
}
if ($module_data[$module]->status) {
// Skip already enabled modules.
unset($module_list[$module]);
continue;
}
$module_list[$module] = $module_data[$module]->sort;
// Add dependencies to the list, with a placeholder weight.
// The new modules will be processed as the while loop continues.
foreach (array_keys($module_data[$module]->requires) as $dependency) {
if (!isset($module_list[$dependency])) {
$module_list[$dependency] = 0;
}
}
}
if (!$module_list) {
// Nothing to do. All modules already enabled.
return TRUE;
}
// Sort the module list by pre-calculated weights.
arsort($module_list);
$module_list = array_keys($module_list);
}
// Required for module installation checks.
include_once DRUPAL_ROOT . '/core/includes/install.inc';
$modules_installed = array();
$modules_enabled = array();
$schema_store = drupal_container()
->get('keyvalue')
->get('system.schema');
$module_config = config('system.module');
$disabled_config = config('system.module.disabled');
foreach ($module_list as $module) {
// Only process modules that are not already enabled.
$enabled = $module_config
->get("enabled.{$module}") !== NULL;
if (!$enabled) {
$weight = $disabled_config
->get($module);
if ($weight === NULL) {
$weight = 0;
}
$module_config
->set("enabled.{$module}", $weight)
->set('enabled', module_config_sort($module_config
->get('enabled')))
->save();
$disabled_config
->clear($module)
->save();
// Load the module's code.
drupal_load('module', $module);
module_load_install($module);
// Refresh the module list to include it.
system_list_reset();
module_implements_reset();
_system_update_bootstrap_status();
// Update the kernel to include it.
// @todo The if statement is here because install_begin_request() creates
// a container without a kernel. It probably shouldn't.
if ($kernel = drupal_container()
->get('kernel', ContainerInterface::NULL_ON_INVALID_REFERENCE)) {
$kernel
->updateModules(module_list(), array(
$module => drupal_get_filename('module', $module),
));
}
// Refresh the schema to include it.
drupal_get_schema(NULL, TRUE);
// Update the theme registry to include it.
drupal_theme_rebuild();
// Allow modules to react prior to the installation of a module.
module_invoke_all('modules_preinstall', array(
$module,
));
// Clear the entity info cache before importing new configuration.
entity_info_cache_clear();
// Now install the module if necessary.
if (drupal_get_installed_schema_version($module, TRUE) == SCHEMA_UNINSTALLED) {
drupal_install_schema($module);
// Set the schema version to the number of the last update provided
// by the module.
$versions = drupal_get_schema_versions($module);
$version = $versions ? max($versions) : SCHEMA_INSTALLED;
// Install default configuration of the module.
config_install_default_config('module', $module);
// If the module has no current updates, but has some that were
// previously removed, set the version to the value of
// hook_update_last_removed().
if ($last_removed = module_invoke($module, 'update_last_removed')) {
$version = max($version, $last_removed);
}
drupal_set_installed_schema_version($module, $version);
// Allow the module to perform install tasks.
module_invoke($module, 'install');
// Record the fact that it was installed.
$modules_installed[] = $module;
watchdog('system', '%module module installed.', array(
'%module' => $module,
), WATCHDOG_INFO);
}
// Allow modules to react prior to the enabling of a module.
entity_info_cache_clear();
module_invoke_all('modules_preenable', array(
$module,
));
// Enable the module.
module_invoke($module, 'enable');
// Record the fact that it was enabled.
$modules_enabled[] = $module;
watchdog('system', '%module module enabled.', array(
'%module' => $module,
), WATCHDOG_INFO);
}
}
// If any modules were newly installed, invoke hook_modules_installed().
if (!empty($modules_installed)) {
module_invoke_all('modules_installed', $modules_installed);
}
// If any modules were newly enabled, invoke hook_modules_enabled().
if (!empty($modules_enabled)) {
module_invoke_all('modules_enabled', $modules_enabled);
}
return TRUE;
}