You are here

public function Vars::saveDefaults in Variable API 6.2

Same name and namespace in other branches
  1. 6 vars.module \Vars::saveDefaults()
  2. 7.2 vars.classes.inc \Vars::saveDefaults()
  3. 7 vars.module \Vars::saveDefaults()

Saves the default value for the variables defined from the module.

Saves the default values for the variables defined from the module implementing a subclass of Vars. The function should be called from the implementations of hook_install(), or hook_update_N().

File

./vars.module, line 407
Implement an API to handle persistent variables.

Class

Vars
@file Implement an API to handle persistent variables.

Code

public function saveDefaults() {
  $vars = $this
    ->getDefaults();
  if (!empty($vars) && is_array($vars)) {
    foreach ($vars as $name => $info) {
      if (!is_array($info)) {
        $info = array(
          'value' => $info,
        );
      }
      $info += array(
        'value' => '',
        'flags' => 0,
      );
      $result = db_fetch_object(db_query_range("SELECT * FROM {variable_default} WHERE module = '%s' AND name = '%s'", $this->module, $name, 0, 1));
      if ($result === FALSE) {
        $var = new stdClass();
        $var->module = $this->module;
        $var->name = $name;
      }
      else {
        $var = $result;
      }
      $var->value = serialize($info['value']);
      $var->flags = $info['flags'];
      drupal_write_record('variable_default', $var, $result === FALSE ? array() : 'vdid');
      $clear_cache = TRUE;
    }
    if (isset($clear_cache)) {
      cache_clear_all('variables', 'cache');
      cache_clear_all('module:' . $this->module, 'cache_vars');
      self::staticReset('vars_default_values');
    }
  }
}