In this post, I'll show you how to override a configuration dynamically in Drupal 8.
You can override configuration from settings.php files using the variable $config
.
Examples:
$config['system.maintenance']['message'] = 'Sorry, our site is down now.';
For nested values, use nested array keys
$config['system.performance']['css']['preprocess'] = 0;
Example of Overriding Payment Gateway configuration for security reasons:
$config['commerce_payment.commerce_payment_gateway.paypal_express_checkout']['configuration'] = [
'api_username' => 'MY_API_USERNAME',
'api_password' => 'MY_API_PASSWORD',
'signature' => 'MY_SIGNATURE',
];
When using $config
override outside of settings.php
, use a preceding global $config;
To get the original values without
override just use:
$message = \Drupal::config('system.maintenance')->getOriginal('message', FALSE);
To get the override values just use:
method 1
$message = \Drupal::config('system.maintenance')->get('message');
method 2
$message = \Drupal::configFactory()->getEditable('system.maintenance')->get('message');