I want to override a custom module configuration in the settings.php file. So the configuration in database can be independent from the environment.

$config['my_module.settings']['prop'] = 'foo';

I my module configuration form, the config result is different given the method I use to get it :

class MyConfigurationForm extends ConfigFormBase {

    public function buildForm( array $form, FormStateInterface $form_state ){
        $config = $this->config( 'my_module.settings' );
        print_r( $config->get('prop') ); 
        // prints database value, or nothing if there is no value in database for 'prop'

        $config = \Drupal::config( 'my_module.settings' );
        print_r( $config->get('prop') ); 
        // prints 'foo'


Why is that ? What's the difference between these two methods ?


In a configuration form use $this->config() and not \Drupal::config().

The difference between both is that the configuration form uses immutable configuration.

When Drupal 8 was in beta, the immutable configuration was introduced to avoid bleeding of overridden values into configuration when saved to the database. See the change record:

Configuration objects by default are immutable

This is the code executed by $this->config() of a configuration form, in a static version:

\Drupal::configFactory()->getEditable('system.site')->set('name', 'My fantastic site')->save();

For Drupal 8.7 it is discussed to display a warning for overridden values:

This form contains values that have been overridden. Changes to these values can still be saved, but the overridden values will take precedence. Overrides are as follows ...

See the issue:

There is no indication on configuration forms if there are overridden values

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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