views:

47

answers:

2

hi,

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'] = FALSE;

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

+2  A: 

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.

Jeremy French
ok 1) where should I implement hook_form_alter() function ? 2) what should be its name node_form_alter() ? (should I include it in back-end theme folder ?)
Patrick
Modules are not themes and have different naming conventions. You will need to have a look at some module development how tos.
Jeremy French
+3  A: 

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

Henrik Opel
Much clearer than my answer. Nice tip about $form['#after_build'] saves messing around with module weights.
Jeremy French
@Jermey French: Yup, $form['#after_build'] can be quite handy. However, if two forms use it, you still end up messing around with module weights :/ (Unless you go one higher and start using '#pre_render' - took me two hours once to grok how CCK managed to completely ignore/override my node field order adjustments ;)
Henrik Opel

related questions