Can I set a form_state redirect with a destination parameter in form submission?
I've built a "Quick Links" drop-down select box for my Drupal-based web app. The select box is implemented as a standard Drupal form, which appears on all pages in the site. When the user selects an option and submits the form, I want to redirect the user to the appropriate page, but have them return to the original page when they have finish开发者_JS百科ed.
e.g. user is on node/5, and selects a quick link which takes them to another form. When they have completed this form, I want them to be returned to node/5.
I have achieved this in other parts of the system using drupal_get_destination to append the ?destination=node/5 querystring parameter to the destination form, and this works nicely. However, in my quicklinks form submit handler, I'm setting form_state['redirect'] to set the target page, and if I add the ?destination= parameter it gets URL-encoded, and so isn't picked up when my target form is submitted.
function dh_seo_quick_links_form_submit($form, &$form_state) {
$form_state['redirect'] = $form['#link_targets'][$link].'?'.drupal_get_destination();
}
Have I gone about this completely the wrong way? Is this redirection even possible? Could I do something clever with the $_REQUEST variables directly?
Here's a nice solution for Drupal 7 which allows you to use relative paths:
$form_state['redirect'] = array(
'node/[node number]',
array(
'query' => array(
'variable_name' => 'value',
),
'fragment' => 'hash_fragment'
),
);
This code creates the following url:
..?node/[node number]&variable_name=value#hash_fragment
This allows you a lot of flexibility while taking advantage of the Drupal framework.
OK - I've worked it out. It seems that if you set a relative URL in the $form_state['redirect'] it gets URL-encoded, but an absolute URL will not:
$form_state['redirect'] = 'myform?'.drupal_get_destination();
redirects to myform%3Fdestination%3Dnode%252F5
global $base_url;
$form_state['redirect'] = $base_url.'/myform?'.drupal_get_destination();
redirects to myform?destination=node/5, which is what I wanted.
You can supply an array for $form_state['redirect']
and put the destination query as second parameter. For example:
$form_state['redirect'] = array(
'myform', array('query' => drupal_get_destination()),
);
You're answer worked for me Mark. Thanks!
You can do it this way also:
global $base_url;
$form['#redirect'] = $base_url .'/'. $form['#redirect'] .'?test=hello';
Finally I got this solution:
global $base_url;
$form_state['redirect'] = $base_url . '/myform?' . drupal_get_destination();
精彩评论