如何在Drupal管理员设置表单上使用Submit回调?


14

我正在使用以下代码制作我的管理表单:

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);
}

但是我注意到,如果我这样做:

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

我不能退出 我猜这是因为要system_settings_form()注意提交。如何获得控制权?我需要在_submit处理程序中进行一些自定义处理...


我尝试添加一个'#submit'属性,$form但这也无济于事。
Aditya MP

3
哦,对不起,确实有效!我做了$form['#submit'] = array('custom_food_orders_admin_submit');,现在可以了。谢谢!
Aditya MP

Answers:


30

$form_id . '_submit'如果未设置其他提交处理程序,则仅触发默认的提交处理程序。

由于system_settings_form()设置了一个提交处理程序,因此不会触发默认处理程序,因此如果要使用它,则必须指定它。

您可以这样做:

/**
 * 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.
}

像这样添加您的提交处理程序意味着您的将在system_settings_form()提交处理程序之前运行。

如果您想让自己的程序运行起来,而不是:

// 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);

做:

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

但是请确保不要这样做:

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

因为这样做会覆盖system_sttings_form()提交处理程序,这首先会破坏使用system_settings_form()函数的目的。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.