Jun 22, 2014

Functions variable_set() and variable_get() are removed

The Optimizely module for D7 uses variable_set() and variable_get() in a number of places for storing and accessing the Optimizely account id number that is used to access the Optimizely service.Those two functions have been removed from Drupal 8.

After considering some of the possible options to replace that code, I decided that the Simple Configuration API would be a good fit since we only need to store and retrieve a single persistent value.

In this API, there are named groups of values. The name of each grouping consists of the module name plus a "subsystem" name, for example, 'optimizely.settings'. Within each group, there are key-value pairs. In our case, there is a single key, optimizely_id, whose value is a positive number.

Accordingly, I wrote a class AccountId to encapsulate the API. The class definition is below and should be mostly self-explanatory.

An object of core class Config corresponds to a named group of values. Once you instantiate an object of this type by calling \Drupal::config(), you can then call its set() and get() methods for specific key-value pairs.

namespace Drupal\optimizely;

class AccountId {

  private static $config = NULL;

  private static function getConfig() {
    if (! self::$config) {
      self::$config = \Drupal::config('optimizely.settings');
    return self::$config;

  public static function getId() {
    $config = self::getConfig();
    $optimizely_id = $config->get('optimizely_id');
    return $optimizely_id;

  public static function setId($id) {
    $config = self::getConfig();
    $config->set('optimizely_id', $id);
    return TRUE;

  public static function deleteId() {
    // N.B. This deletes any and all settings
    // stored in "optimizely.settings".

    $config = self::getConfig();
    return TRUE;

This class is then used, for example, in AccountSettingsForm::buildForm() to set the default value for the Optimizely id form field,
'#default_value' => AccountId::getId(),
and then in AccountSettingsForm::submitForm() to save the new id,
$optimizely_id = $form_state['values']['optimizely_id'];
  - - - - -
As originally implemented, each group of values is stored in a YAML file. Such files are still used for import and export purposes and for synchronizing configuration across different Drupal sites.

However, during normal usage, the API storage is only in the database, with no YAML files needed. In the database, table config contains all the groups of values. The name column has the group names. The corresponding data column contains all the individual values for that group.

  - - - - -
Besides the Optimizely account id, I encountered one other use of variable_get().
$front_path = variable_get('site_frontpage');
After searching for the string 'site_frontpage' in the core code for both D7 and D8, examining the search results, and doing a bit of testing, I replaced the above line of code with
$config = \Drupal::config('system.site');
$front_path = $config->get('page.front');
  - - - - -

Update: See  Beta 6: ConfigFactoryInterface::getEditable()


Overview of Configuration (vs. other types of information)

Simple Configuration API
Very useful for concepts and actual examples of code.

Configuration Storage in Drupal 8
Configuration file format, file location, and use of YAML files.

No comments:

Post a Comment