7

I am trying to enable modules by adding them to core.extension.yml file without getting involved with Features, Configuration Installer or similar modules or profiles. The modules are enabled but their default configuration under module_folder/config/install or module_folder/config/optional are not imported. Is there a way to do this?

7
  • 3
    So when exactly do you want to install a module? Installing a module through core.extension is perfectly valid if you're doing it as part of an config import. But then you have to provide all the configuration of the module also in the config sync folder. Usually you install the module somewhere on a test site, optionally customize the configuration, then export that, deploy it and import. You don't need features or anything like that. And you only need Config Installer if you try to install a completely new site through tat
    – Berdir
    Aug 23, 2017 at 22:07
  • 1
    @Berdir thank you man, I was thinking that there is no one around here anymore after a few unanswered questions. Faith in Drupal Answers restored:) -- Since the projects I am doing is not that big and I am very lazy, I am not setting up clones for such testing purposes. But yes that's an ideal workflow. As of the Config Installer, I saw some issues in the queue and didn't wanna get involved with the module since it could give me more headache then manually installing 25 modules :)
    – duru
    Aug 24, 2017 at 9:30
  • How do you solve your problem? Sep 7, 2017 at 16:58
  • @AdrianCidAlmaguer hi, thanks for the follow up. I haven't tried any of them yet. I'll try in my next project and select an answer.
    – duru
    Sep 8, 2017 at 15:02
  • @duru well just publish your solution, I think that this is a good question ;-) Sep 8, 2017 at 15:46

2 Answers 2

4

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.

3
  • @duru a pleasure help you, at the end lets us know which method you chose and why. Aug 24, 2017 at 14:06
  • 1
    looks like @duru is still thinking about which answer to "chose" (= mark as accepted?) ... Jan 22, 2018 at 21:37
  • 1
    Mind that hook_install will run on module installation only. If your module is already installed, use hook_update_N like CG Monroe answer.
    – wranvaud
    Feb 4, 2021 at 18:07
4

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.

2
  • Cool. Let's see if there will be other answers.
    – duru
    Aug 24, 2017 at 9:33
  • 2
    @duru If you install as a dependency then can't uninstall any module because all are needed by your module (the module that install all the other modules), and if you use the hook_update() then you need to install the module and after run the update. This is why I think that is better put the code in the .install file, but it depends from your requirements. Aug 24, 2017 at 12:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.