I am creating a simple module to learn more about Drupal. I am having a problem with the administration form of my module, where I use variable_get to populate some radios and checkboxes.

The form is built like this:

<?php
  $form['kwblock'] = array(
    '#type' => 'fieldset',
  );
  $form['kwblock']['krkhwebcal_block'] = array(
    '#type' => 'radios',
    '#title' => t('Block'),
    '#default_value' => variable_get('krkhwebcal_block_display', 0),
    '#options' => array(t('All'), t('Authenticated'), t('Hide')),
    '#description' => t('Show the block for all visitors, only authenticated users, or hide for all.'),
  );

  $form['kwsignups'] = array(
    '#type' => 'fieldset',
  );
  $form['kwsignups']['krkhwebcal_open'] = array(
    '#type' => 'radios',
    '#title' => t('Signup for events'),
    '#default_value' => (int)variable_get('krkhwebcal_open_signups', 1),
    '#options' => array(1 => 'Enabled',0 => 'Disabled'),
  );
  $form['kwsignups']['krkhwebcal_anonymous'] = array(
    '#type' => 'checkbox',
    '#title' => t('Anonymous signups'),
    '#default_value' => (int)variable_get('krkhwebcal_anonymous_signup', 0),
    '#description' => t('Allow anonymous visitors to sign up.'),
  );

  return system_settings_form($form);
?>

The first time this form is deployed, it is naturally populated with the default values from variable_get, but if I save other values (and I confirm that they are actually stored in the variable table), the form is still populated with the default values. What am I doing wrong?

Comments

portablecow’s picture

Did you ever figure this out? I'm having the same problem.

tjodolv’s picture

Not exactly, because I went for a different approach to the whole solution, but I was told that for variable_get() to work, the variable must initially be set with variable_set(). I did not try it myself, but it's worth a shot..?

bwho’s picture

From what I can tell...

When you use system_settings_form you've got to make sure your variable name in variable_get($var, $default_value) is the same as the one in $form_state['value'][$var].

I also found that it helps to format the $default_value for variable_get() a bit (this example is done with checkboxes, but I am guessing this will work across form #types). Like:

$keyed_array = array(
  0 => t('Option 1'),
  1 => t('Option 2'),
  ...
);

...

$form['some_fieldset'][$var] = array(
  '#type' => 'checkboxes',
  '#title' => t('Some instructions or the title here.'),
  '#options' => $keyed_array,
  '#default_value' => variable_get($var, array($key, $value)),
);

...

Awesomely enough, if you set the form up correctly, Drupal will store and recall your variable, even if it has to be serialized. And don't forget to clean up your vars in your_module.install with hooks install and uninstall, and variable_set() and variable_del() respectively.

semperos’s picture

Yes, that is correct. And I forget it every time :) Thanks for posting this. The name of the form element in your FAPI array must match the name you use with variable_get for it to do its save-serialize-deserialize magic. +1 for mentioned module.install cleanup functions.