0

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?

7
  • 2
    You can simply edit settings.php why would you need it to be updated from some hook?
    – leymannx
    May 31, 2022 at 5:57
  • Please check below for the reason I wanted this but looks like not possible. Jun 1, 2022 at 10:08
  • 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.
    – leymannx
    Jun 1, 2022 at 10:12
  • 1
    @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. Jun 2, 2022 at 4:53
  • 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.
    – leymannx
    Jun 2, 2022 at 13:22

1 Answer 1

0

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.

5
  • 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. May 31, 2022 at 11:18
  • 3
    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
    – Clive
    May 31, 2022 at 11:31
  • Yes, Settings are by design read-only. Only in tests they replace the entire singleton instance. For example KernelTestBase::setUpFilesystem
    – 4uk4
    May 31, 2022 at 11:42
  • Thanks to all for the clarifications. Jun 1, 2022 at 10:06
  • With cli drush config-import --source=SOURCE you can define specific config folders.
    – Rafael
    Jun 3, 2022 at 6:57

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.