You are here

public function Vars::saveDefaults in Variable API 7

Same name and namespace in other branches
  1. 6.2 vars.module \Vars::saveDefaults()
  2. 6 vars.module \Vars::saveDefaults()
  3. 7.2 vars.classes.inc \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 293
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,
      );
      try {
        db_merge('variable_default')
          ->key(array(
          'module' => $this->module,
          'name' => $name,
        ))
          ->fields(array(
          'value' => serialize($info['value']),
          'flags' => $info['flags'],
        ))
          ->execute();
      } catch (Exception $e) {
      }
      $clear_cache = TRUE;
    }
    if (isset($clear_cache)) {
      cache_clear_all('variables', 'cache_bootstrap');
      cache_clear_all('module:' . $this->module, 'cache_vars');
      drupal_static_reset('vars_default_values');
    }
  }
}