处理表单时,的值将$form_state['redirect']发送到drupal_goto(),并且drupal_goto()始终优先$_GET['destination']于其自身的$path参数。
为了完整起见,在Drupal 6中,如果没有在hook_exit()以下位置设置自己的标头,则有些不走运:
function mymodule_exit($destination = NULL) {
$my_destination = 'foo/bar';
header('Location: ' . url($my_destination));
exit;
}
在Drupal 7中,hook_drupal_goto_alter()为此特定用例添加了:
function mymodule_drupal_goto_alter(&$path, &$options, &$http_response_code) {
$path = 'foo/bar';
}
Drupal 7的另一个更接近您要执行的操作的选项是,drupal_get_destination() 使用以下命令在您的提交处理程序中重置静态缓存drupal_static_reset():
function mymodule_form_submit($form, &$form_state) {
// See note
$form_state['redirect'][] = drupal_get_destination();
$form_state['redirect'][] = 'foo/bar';
unset($_GET['destination']);
drupal_static_reset('drupal_get_destination');
drupal_get_destination();
}
由于您drupal_get_destination()在重置后立即调用,因此Drupal非常高兴地不知道其余页面构建的目标参数,包括它何时调用drupal_goto()。
注意:我更改了定义代码,$form_state['redirect']因为您永远不想覆盖变量:其他提交处理程序可能已经定义了自己的重定向。Drupal将始终使用数组中的最后一项,因此,如果您要foo/bar覆盖目标参数(以及到此为止定义的所有其他重定向),则它必须为最后一个。