Drupal: $form['#redirect'] = FALSE; doesn't work
I've tried to change the redirection when I submit my edit-node form, by addming the following line to my template.php file, in my theme
$form['#redirect'] = F开发者_运维技巧ALSE;
I'm sure the template.php file works well because I have other lines in which I change, for example, the weights of some elements. But the redirection doesn't work.
I've also tried $form['#redirect'] = 'anotherPage'; without success.
What am I doing wrong ? I'm following the Drupal APIs, about forms: http://api.drupal.org/api/drupal/developer--topics--forms_api_reference.html/6#redirect
thanks
IIRC, the $form['#redirect']
entry will only be effective if no other redirection is set later on in the form processing. If you take a look at node_form_submit()
, you can see that it sets its own redirection via $form_state['redirect'] = 'node/'. $node->nid;
, thus overriding the redirection you defined in the form definition earlier on.
You can work around this by adding your own submit handler callback to the $form['#submit']
array (needs to be placed after the default one). Within that callback, you can change the $form_state['redirect']
to your desired path.
NOTE: If the $form['#submit']
array does not yet contain the default entry during hook_form_alter()
, you might need to add another indirection by adding a callback to $form['#after_build']
- within that callback, you have a final chance to manipulate the form array before it gets rendered. (Well, almost final - there is still $form['#pre_render']
later on ;)
You should create a module and use hook_form_alter() to alter the form before it is displayed. Generally you shouldn't manipulate data and behaviour in the theming layer.
You may be able to use drupal_rebuild_form(), in the .tpl file to avoid this, but I don't know what other consequences this will have.
精彩评论