0

So, I have a form with a default value 'You have not enabled TFA' that is editable for the user. This works, and user input is recorded in the database, but when the form is returned it shows the default value again, and I think it would be better to return the form with the inputted values.

Here is my admin.inc file:

function my_module_admin_settings_form($form, $form_state, $nid = 0){
  $form = array();
  $form['subject'] = array(
    '#title' => t('Subject Line'),
    '#descirption' => t('Default email subject line.'),
    '#type' => 'textfield',
    '#size' => '50',
    '#required' => TRUE,
    '#default_value' => variable_get('subject', 'You have not enabled TFA'),
  );

...

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );

  return $form;
}

Here is the code for variable_get function (for reference):

function variable_get($name, $default = NULL) {
  global $conf;

  return isset($conf[$name]) ? $conf[$name] : $default;
}

Can anybody see what is going on?

0

3 Answers 3

2

If I understand your question correctly, your values are not saved.

This is because you either need to handle storing variables (using variable_set()) in your submit callback, or use system_settings_form() to do this for you.

The latter approach is generally used for administrative settings, unless you need some additional processing or storing part of the settings somewhere else.

Do note that with system_settings_form() you don't need to add the form actions - they will be added for you.

Here's an example:

function my_module_admin_settings_form($form, $form_state, $nid = 0){
  $form = array();
  $form['subject'] = array(
    '#title' => t('Subject Line'),
    '#descirption' => t('Default email subject line.'),
    '#type' => 'textfield',
    '#size' => '50',
    '#required' => TRUE,
    '#default_value' => variable_get('subject', 'You have not enabled TFA'),
  );

  // Add more fields.

  return system_settings_form($form);
}
2
  • yes, thank you this is exactly what I was looking for Apr 7, 2016 at 15:09
  • You're welcome - glad it helped. Apr 7, 2016 at 15:30
0

Do check that $form['subject'] not repeated in any where of the form because $form['subject'] should always be unique .

And yes you need to use return system_settings_form($form); to store in variable table

0

You need to return form wrapped to system_settings_form, and also its recommended to add prefix with your module name to your variable. This an example with menu hook.

/**
 * Implements hook_menu().
 */
function my_module_menu() {
  $items['admin/config/system/my-settings'] = array(
    'title' => 'My settings',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('my_module_settings'),
    'access arguments' => array('administer site configuration'),
    'weight' => 20,
  );

  return $items;
}

/**
 * Form builder; Settings form.
 */
function my_module_settings() {
  $form['my_module_subject'] = array(
    '#title' => t('Subject Line'),
    '#descirption' => t('Default email subject line.'),
    '#type' => 'textfield',
    '#size' => '50',
    '#required' => TRUE,
    '#default_value' => variable_get('my_module_subject', 'You have not enabled TFA'),
  );

  return system_settings_form($form);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.