n Override a configuration dynamically in Drupal 8 | CodimTh

Please Disable Your Browser Adblock Extension for our site and Refresh This Page!

our ads are user friendly, we do not serve popup ads. We serve responsible ads!

Refresh Page
Skip to main content
On . By CodimTh
Category:

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');

Riadh Rahmi

Senior Web Developer PHP/Drupal & Laravel

I am a senior web developer, I have experience in planning and developing large scale dynamic web solutions especially in Drupal & Laravel.

Web Posts

Search

Page Facebook