Drupal - How to enable modules through configuration?

This will not works because you are only saying to Drupal that your module is installed, but when you install a module there are other process that need to runs to import the configs and execute the .install functions.

But you can create a new module that will install all the other modules, create a .install file and add this inside:

/**
 * Implements hook_install().
 */
function MY_MODULE_install() {
  $modules_list = [
    'nbsp',
    'typogrify',
    'other_module',
  ];
  \Drupal::service('module_installer')->install($modules_list);
  drupal_flush_all_caches();
}

And then you only need to install this module that will install all the other modules.


A possible alternative would be use the dependencies section of a module's mymod.info.yml description. Enabling the module will enable the dependencies.

name: My Module
description: Provides something great
core: 8.x
type: module
dependencies:
  - views

If you need to make sure that certain modules are enabled due to code changes in an existing module, then use update system. E.g. add a hook_update_N function to the mymod.install file that calls the module.installer service.

/**
 * Install file module.
 */
function mymod_update_8001() {
  \Drupal::service('module_installer')->install(['file']);
}

FYI - Trying to install an installed module will not cause problems.

Trying to go outside the normal system by accessing the core.extensions is probably not a long term maintainable option.