add custom submit to system setting form drupal 7

The default submit handler of $form_id . '_submit' only fires if no other submit handlers have been set.

Because system_settings_form() sets a submit handler, the default handler doesn’t fire, so you have to specify it if you want to use it.

You can do so like this:

/** * A custom form. */ function custom_form() { $form = array(); $form['custom_form_field'] = array( '#type' => 'textfield', '#title' => t('Custom form field'), '#default_value' => variable_get('custom_form_field', ''), ); // Set a submit handler manually because the default submit handler // gets overridden by the system_settings_form() submit handler. $form['#submit'][] = 'custom_form_submit'; return system_settings_form($form); } /** * Submit handler for the custom form. */ function custom_form_submit($form, &$form_state) { // Submit code here. }

Adding your submit handler like this means yours will run before the system_settings_form() submit handler.

If you want yours to run after, instead of:

// Set a submit handler manually because the default submit handler // gets overridden by the system_settings_form() submit handler. $form['#submit'][] = 'custom_form_submit'; return system_settings_form($form);

Do:

$form = system_settings_form($form); $form['#submit'][] = 'custom_form_submit'; return $form;

Make sure not to do this though:

$form = system_settings_form($form); $form['#submit'] = array('custom_form_submit'); return $form;

because that would overwrite the system_sttings_form() submit handler, which would defeat the purpose of using the system_settings_form() function in the first place.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

WordPress.com.

Up ↑

%d bloggers like this: