Feb 22, 2015

Beta 6: ConfigFactoryInterface::getEditable()

Migrating to D8 beta 6 (from beta 4), when I tried to submit a form to change a field value that is persisted by using the Simple Configuration API, I got the log error message,

Can not set values on immutable configuration ... Use \Drupal\Core\Config\ConfigFactoryInterface::getEditable() ...

As of beta 6, by default, the values of configuration objects cannot be changed. So the line of code from the previous release,

  $config = \Drupal::config('optimizely.settings');

had to be replaced with the following in order to retrieve a mutable object.

  $config =
    \Drupal::configFactory()
      ->getEditable('optimizely.settings');

Calls to get() and set() then work as expected.

  $id = $config->get('optimizely_id');

  $config->set('optimizely_id', $id);


Sources:

Configuration objects by default are immutable.
https://www.drupal.org/node/2407153

Working with Configuration Forms
https://www.drupal.org/node/2206607

Functions variable_set() and variable_get() are removed
http://optimizely-to-drupal-8.blogspot.com/2014/06/functions-variableset-and-variableget.html