8

A friend just showed me system_settings_form in action in a configuration file for a form.

The code in question looks like this (simplified).

function my_module_generateAdminForm($form){
  $form = array();
  $form['super_secret_password'] = array(
    '#type' => 'textfield',
    '#title' => t('Headline'),
    '#default_value' => variable_get('super_secret_password','password'),
  );
  system_settings_form($form);
}

system_settings_form adds in submit buttons and handles the form submission, saving the variables. It also adds a "Reset to default" button - that resets all the variable to the default.

I basically guessed what it does because there's not a whole lot of documentation out there and my friend doesn't know exactly what it does either. My question is how does it know the variables to save/reset? Aren't I just passing an array?

Also is there any other magic in system_settings_form that I should know about?

1 Answer 1

14

See system_settings_form and system_settings_form_submit for implementation details. In Drupal 7 Reset to Default button is removed from System Settings form .

function system_settings_form($form) {
  $form['actions']['#type'] = 'actions';
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save configuration'),
  );

  if (!empty($_POST) && form_get_errors()) {
    drupal_set_message(t('The settings have not been saved because of the errors.'), 'error');
  }
  $form['#submit'][] = 'system_settings_form_submit';
  // By default, render the form using theme_system_settings_form().
  if (!isset($form['#theme'])) {
    $form['#theme'] = 'system_settings_form';
  }
  return $form;
}

System_settings_form :

Add's save configuration Submit button and submit callback to system_settings_form_submit function.

system_settings_form_submit:

function system_settings_form_submit($form, &$form_state) {
  // Exclude unnecessary elements.
  form_state_values_clean($form_state);

  foreach ($form_state['values'] as $key => $value) {
    if (is_array($value) && isset($form_state['values']['array_filter'])) {
      $value = array_keys(array_filter($value));
    }
    variable_set($key, $value);
  }

  drupal_set_message(t('The configuration options have been saved.'));
}

Iterates through all form elements and sets variables for each form element with key as form element key and value as user submitted form element value read using $form_state['values']

So, it's just a form with submit handler that sets variables for all form values using variable_set function.

3
  • 5
    Good explanation. If you are confused by "each form element with key as form element key", what he means is that in your example above, $form['super_secret_password'] will cause Drupal to call variable_set('super_secret_password', ...), because system_settings_form_submit takes the name of the variable from the array key ('super_secret_password') of your form. Commented Oct 29, 2012 at 4:41
  • 1
    This is helpful. Why can't api.drupal.org just explain what it does in plain english. Instead it just gives the code... So in summary not voodoo, just using my field keys.
    – Coomie
    Commented Oct 29, 2012 at 5:19
  • 1
    I think it's worth noting that you should not use this to store large variables as it will stay in memory for every page request no matter there was a direct variable_get for it or not. They all are cached once in memory. Also, regular submit and validate handlers will work without a problem. Feel free to add them.
    – AKS
    Commented Oct 29, 2012 at 7:27

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.