13

I'm using this code to make my admin form:

function custom_orders_admin(){
  $form = array();

  $form['custom_ignore_terms']['ignore_terms'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Don\'t display these'),
    '#default_value' => variable_get('custom_ignore_terms', array()),
    '#options' => drupal_map_assoc($termTypes),
    '#description' => t("Choose the terms you do not want displayed."),
    '#required' => FALSE,
  );

  return system_settings_form($form);
}

But I notice that if I do this:

function custom_orders_admin_submit($form, &$form_state) {
  exit(var_dump($form_state));
}

I can't exit. I'm guessing this is because system_settings_form() takes care of submitting. How do I get my control back? I need to do some custom processing in that _submit handler...

  • I tried adding a '#submit' property to $form but that didn't help either. – Aditya M P Apr 13 '12 at 4:36
  • 3
    Uh oh, sorry that actually worked! I did $form['#submit'] = array('custom_food_orders_admin_submit'); and now it's working. Thank you! – Aditya M P Apr 13 '12 at 4:42
29

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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