Score:0

Is it possible to programmatically change the config_sync_directory path after profile installation?

cn flag

Installing Drupal 9 using a custom profile installation, I have defined "config_sync_directory" in settings.php file in $settings array to pick up the profile config during installation.

  1. During site installation Drupal asks to specify the "config" directory in the settings file
  2. In a custom profile I have some default configurations, I want Drupal to read configurations from that directory during installation, the path I specified in the settings file.
  3. Now, post-installation I want to specify a separate config sync directory, and do not want to edit the settings.php file, again and again, just wanted to update the sync directory path from a hook in the custom module.

Is it possible to change the path programmatically from a hook?

leymannx avatar
ne flag
You can simply edit settings.php why would you need it to be updated from some hook?
Prashant Chauhan avatar
cn flag
Please check below for the reason I wanted this but looks like not possible.
leymannx avatar
ne flag
You can update that value any time after installation by just editing settings.php. I don't see any explanation that clarifies why you ain't able to do so. Simply open settings.php in the editor of your choice (maybe clear file permissions first), edit that value, save (and maybe strengthen file permission again). Just ensure you also move the config directory to the new location. That's all.
Prashant Chauhan avatar
cn flag
@leymannx Actually what I wanted to achieve was 1. During site installation Drupal asks to specify the "config" directory in the settings file 2. In a custom profile I have some default configurations, I want Drupal to read configurations from that directory during installation, the path I specified in the settings file. 3. Now, post-installation I want to specify a separate config sync directory, and do not want to edit the settings.php file, again and again, just wanted to update the sync directory path from a hook in the custom module. Hope I made the requirements clear.
leymannx avatar
ne flag
When you install a profile its config gets read from the profile's config directory regardless of what you configured in settings.php. And it's really important that you add that to your question. Otherwise you won't get the answer you are looking for.
Prashant Chauhan avatar
cn flag
@leymannx That is not the case, during installation it reads config from the config path given in the settings file.
leymannx avatar
ne flag
If that's not working during installation, then something's wrong in your profile. Check other profile's (minimal, standard) config directories on where the config is supposed to be exactly.
Score:0
us flag

Modules can override the values in configuration objects. As described in Configuration override system / Providing overrides from modules, a module needs to implement a service tagged config.factory.override. The class used for the service needs to implement ConfigFactoryOverrideInterface, as the example given in the documentation does.

services:
  config_example.overrider:
    class: Drupal\config_example\Config\ConfigExampleOverrides
    tags:
      - {name: config.factory.override, priority: 5}
namespace Drupal\config_example\Config;

use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Config\ConfigFactoryOverrideInterface;
use Drupal\Core\Config\StorageInterface;

/**
 * Example configuration override.
 */
class ConfigExampleOverrides implements ConfigFactoryOverrideInterface {

  /**
   * {@inheritdoc}
   */
  public function loadOverrides($names) {
    $overrides = [];
    if (in_array('system.site', $names)) {
      $overrides['system.site'] = ['name' => 'Overridden site name!'];
    }
    return $overrides;
  }

  /**
   * {@inheritdoc}
   */
  public function getCacheSuffix() {
    return 'ConfigExampleOverrider';
  }
  
  /**
   * {@inheritdoc}
   */
  public function getCacheableMetadata($name) {
    return new CacheableMetadata();
  }

  /**
   * {@inheritdoc}
   */
  public function createConfigObject($name, $collection = StorageInterface::DEFAULT_COLLECTION) {
    return NULL;
  }

}

Keep in mind that the value in the settings.php file always override the module values. For a module to set a configuration value, the settings.php file needs not to set it.

Prashant Chauhan avatar
cn flag
I think you misunderstood my question. I am defining the config sync folder path in settings.php which is working properly. Now after that once Drupal is installed successfully I want to update this config sync folder path and it is not a config, it is a Setting.
cn flag
There's no hook for overwriting global settings if that's what you mean. There's no need for one, you can just edit settings.php to reflect the desired new value instead of editing a hook in a module file
4uk4 avatar
cn flag
Yes, Settings are by design read-only. Only in tests they replace the entire singleton instance. For example [KernelTestBase::setUpFilesystem](https://api.drupal.org/api/drupal/core%21tests%21Drupal%21KernelTests%21KernelTestBase.php/function/KernelTestBase%3A%3AsetUpFilesystem)
Prashant Chauhan avatar
cn flag
Thanks to all for the clarifications.
Rafael avatar
uz flag
With cli drush config-import --source=SOURCE you can define specific config folders.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.