0

I am looking to set default configuration settings for a contributed module, Nodeaccess, on install. I know that you can do that on site spin-up but this module will not be enabled by default. I tried creating a nodeaccess.settings.yml and placing it in the config/install folder but when I go to enabled the Nodeaccess module, an error notification appears: Unable to install Nodeaccess, nodeaccess.settings already exists in active configuration.

Is there a way to set default configuration settings for Nodeaccess on install? Thanks for your time.

-Chad

2
  • The Configuration Synchronization system in Drupal Core is designed to do this in some common workflows: dev/staging to production. But I guess this question is about some other workflow.
    – mona lisa
    Commented May 9 at 18:02
  • Sounds like uninstalling this module is not deleting the config or perhaps it’s due to using the config split contrib module.
    – No Sssweat
    Commented May 10 at 11:26

2 Answers 2

2

There are several options.

You can use a hook_install() in a custom module which installs the contrib module:

mymodule.install

/**
 * Implements hook_install().
 */
function mymodule_install() {
  $config = \Drupal::configFactory()->getEditable('nodeaccess.settings');
  if ($config) {
    $config
      ->set('config.key', $value)
      ->save();
  }
}

Or react on the installation of the contrib module when it happens:

mymodule.module or myprofile.profile

/**
 * Implements hook_modules_installed().
 */
function mymodule/myprofile_modules_installed($modules, $is_syncing) {
  if ($is_syncing) {
    return;
  }

  if (in_array('nodeaccess', $modules)) {
    $config = \Drupal::configFactory()->getEditable('nodeaccess.settings');
    if ($config) {
      $config
        ->set('config.key', $value)
        ->save();
    }
  }
}

This hook can also be implemented in a profile.

3
  • I'm aware of this function since I use it for custom modules but nodeaccess is a contrib module. Where would this be placed in the code base? Thanks for the response!
    – Chad
    Commented May 9 at 19:04
  • In a custom module. The first draft of the answer was for the case where you install a module and the default config object is already present. Then I re-read the question and it looked more like you want to install your own module and set the config of a contrib module. This seemed more likely because you can't put your yaml file in a contrib module.
    – 4uk4
    Commented May 9 at 19:09
  • ... or in a custom profile. see the edit.
    – 4uk4
    Commented May 10 at 9:05
-1

The steps you followed are the right way to do it. It's how core modules and contrib work.

The error you are getting is probably because other module has registered a configuration with the same key, and keys of course need to be unique. The configuration key is the name of the configuration file, in your case is nodeaccess.settings. This situation is rare but can happen, and is done by design as a failsafe mechanism to avoid other modules messing around, meaning you cannot provide a configuration file for Views module for example. And you should not be doing so, since modifying configuration managed by other module is not a very good practice.

Additionally, as you can see you can install, modify or even delete configurations using the hook system. You may be tempted to so, but I wouldn't recommend installing configurations by "hand" using hook_install. Those operations are designed as way to update module's configuration over time when they are been used in live sites, not as a way of shipping configuration.

Finally, the configuration files provided by modules are only read when the module is first installed. After that that configuration belongs to the site meaning afterwards is managed by the Configuration Manager workflow with the import (read from file, write to database)/export (read from database, write to file) features. At this point the files reside on the config sync directory (this is set on the settings.php file).

Please, find more information about how the configuration works here. Also worth checking this other link, since is related how configuration is handled when performing updates.

Your Answer

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

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