(sorry for my english)

It's also applicable for 6.x


/**
 * This add-on allows for a module:
 * - To specify structure and default values of all module settings in one place: "hook_get_settings()".
 * - Use variable naming standards: <module_name> "_" <setting_name>.
 * - Use defined default values everywhere (with option to override default value)
 *   Example:
 *     Instead of "variable_get('my_module_var1', 456)" in several places of a code, you can just use "setting_get('my_module', 'var1')".
 *     To override default value use the same approach as for "variable_get": "setting_get('my_module', 'var1', 987)".
 * - Automatic check for existence of setting at the moment of "set" or "get" and in case if setting it's not defined raise an error.
 * - You will never forget about to delete some variable at "hook_uninstall()", because you can just call "setting_delete_all('my_module')".
 * - Automatic specify default values ('#default_value') for admin settings form by using "setting_prepare_settings_form('my_module', $form)",
 *   this will also automatically rename element names from format '<setting_name>' to format '<module_name> "_" <setting_name>'
 *   (for proper processing with "system_settings_form_submit()").
 */

function setting_get_settings($module) {
  global $settings;

  if (!isset($settings[$module])) {
    $settings[$module] = module_invoke($module, 'get_settings');
    if (!isset($settings[$module])) {
      $settings[$module] = array();
    }
  }

  return $settings[$module];
}

function setting_check($module, $name) {
  $module_settings = setting_get_settings($module);

  if (isset($module_settings[$name])) {
    return TRUE;
  }
  else {
    trigger_error(t('Setting %name in module %module is not exist.', array('%name' => $name, '%module' => $module)), E_USER_WARNING);
  }
}

function setting_get($module, $name, $value = NULL) {
  if (setting_check($module, $name)) {
    if (!isset($value)) {
      $module_settings = setting_get_settings($module);
      $value = $module_settings[$name];
    }
    return variable_get($module.'_'.$name, $value);
  }
}

function setting_set($module, $name, $value) {
  if (setting_check($module, $name)) {
    variable_set($module.'_'.$name, $value);
  }
}

function setting_get_all($module, $values) {
  global $conf;

  $result = array();
  $module_settings = setting_get_settings($module);
  foreach ($module_settings as $name => $default_value) {
    $result[$name] = (array_key_exists($module.'_'.$name, $conf) ? $conf[$module.'_'.$name] : (array_key_exists($name, $values) ? $values[$name] : $default_value));
  }
  return $result;
}

function setting_reset_all($module) {
  $module_settings = setting_get_settings($module);
  foreach ($module_settings as $name => $value) {
    variable_set($module.'_'.$name, $value);
  }
}

function setting_delete_all($module) {
  $module_settings = setting_get_settings($module);
  foreach (array_keys($module_settings) as $name) {
    variable_del($module.'_'.$name);
  }
}

function setting_prepare_settings_form($module, $form) {
  $result_form = $form;
  foreach ($form as $name => $element) {
    // If this is a form element
    if (($name{0} != '#') && is_array($element)) {
      // If this is an input form element
      if (in_array($element['#type'], array('checkbox', 'checkboxes', 'date', 'file', 'password', 'radio', 'radios', 'select', 'textarea', 'textfield', 'weight', 'hidden'))) {
        // If "default_value" applicable
        if (!in_array($element['#type'], array('file', 'password'))) {
          $element['#default_value'] = setting_get($module, $name);
        }

        // Rename element name in result form
        $result_form[$module.'_'.$name] = $element;
        unset($result_form[$name]);
      }
      else {
        $result_form[$name] = setting_prepare_settings_form($module, $element);
      }
    }
  }
  return $result_form;
}

Use example (exerpts from BitTorrent module):

/**
 * Implementation of hook_get_settings().
 */
function bt_torrent_get_settings()
{
  global $base_url;

  return array(
    // Settings (Public)
    'override_announce' => 1,
    'override_announce_url' => module_exists('bt_tracker') ? $base_url .'/announce.php' : '',
    'scrape_cron' => 0,
    'scrape_cron_interval' => 7200,
    'scrape_cron_count' => 3,
    'scrape_cron_timeout' => 10,

    // Variables (Private)
    'last_scrape' => 0,
    'last_scrape_position' => 0,
  );
}

//...

function bt_torrent_admin_settings() {
  $form['announce'] = array(
    '#type' => 'fieldset',
    '#title' => t('Announce URLs'),
    '#description' => t('Control where the torrents\' URLs will point.'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
  $form['announce']['override_announce'] = array(
    '#type' => 'radios',
    '#title' => t('Override Announce URL'),
    '#options' => array(
      t('Leave Intact'),
      t('Append'),
      t('Override'),
    ),
    '#description' => t('This determines whether or not the announce URLs of the torrents being downloaded should be changed. Append will simply add your URL to the torrent\'s list of announce URLs, where Override will replace the current announce URL(s)'),
  );
  $form['announce']['override_announce_url'] = array(
    '#type' => 'textfield',
    '#title' => t('New Announce URL'),
    '#description' => t('This is the URL to use for the torrents being uploaded.')
  );

  $form['cron'] = array(
    '#type' => 'fieldset',
    '#title' => t('CRON Jobs'),
    '#description' => t('Controls what CRON jobs run within this module.'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
  $form['cron']['scrape_cron'] = array(
    '#type' => 'radios',
    '#title' => t('Scrape other trackers'),
    '#options' => array(
      t('Do not scrape'),
      t('Scrape for statistics'),
    ),
    '#description' => t('Use CURL to load the statistics for torrents that are not tracked locally.'),
  );
  $form['cron']['scrape_cron_interval'] = array(
    '#type' => 'textfield',
    '#title' => t('Scrape Interval'),
    '#description' => t('If enabled, how often the tracker should scrape other trackers.'),
  );
  $form['cron']['advanced'] = array(
    '#type' => 'fieldset',
    '#title' => t('Advanced'),
    '#description' => t('Advanced settings for the cron run.  Note that altering these may make the cron run take longer to run.  Only alter these if you are certain that your php_execute_time is large enough to handle it.'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $form['cron']['advanced']['scrape_cron_count'] = array(
    '#type' => 'textfield',
    '#title' => t('Scrapes per cron'),
    '#description' => t('How many trackers to scrape per cron run.'),
  );
  $form['cron']['advanced']['scrape_cron_timeout'] = array(
    '#type' => 'textfield',
    '#title' => t('Scrape timeout'),
    '#description' => t('How many seconds to wait for a tracker to be scraped.  Make sure that "Scrapes per cron" multiplied by "Scrape timeout" is less than your php_execute_time.'),
  );
  return system_settings_form(setting_prepare_settings_form('bt_torrent', $form));
}

Making pathes is difficult for me, someone who interested in this add-on, please make a patch.

Comments

damien tournoud’s picture

Status: Needs review » Closed (duplicate)

Please see #145164: DX: Use hook_variable_info to declare variables and defaults for a discussion of a similar approach.

liquixis’s picture

Thanks for link.