i am new to Drupal. I have few nodes with term reference. After creating nodes with term reference, i want to redirect user not on created node, but on term page. Is this possible?
I want to redirect my users after they create a piece of new content, instead of directing them to the 'view' page for the content.
I have seen mention of using hook_alter or something, but I'm really new to drupal and not even sure what that means?
Thanks!
As you mention that you are new to Drupal, I'd suggest to take a look at the Rules module. You can add a trigger on for content has been saved/updated and add an action, to redirect the user to a specific page.
You can however do the same in a light weight custom module using a form_alter hook.
First, find the form ID of the form. For node forms, it's the [node-type]_node_form. Then, you can add a new submit function to be executed when the form is submitted. In this submit handler, set the redirect path.
See this guide on a basic how-to on creating a module. Your module code would be something like belows:
<?php
function mymodule_mytype_node_form_alter(&$form, &$form_state) {
//下面两行代码都试一下吧 两个 都试
$form['actions']['submit']['#submit'][]= 'mymodule_node_submit_do_redirect'; //有时用它正确
$form['#submit'][] = 'mymodule_node_submit_do_redirect'; //有时用它正确
}
function mymodule_node_submit_do_redirect($form, &$form_state) {
$form_state['redirect'] = 'my_custom_destination';
}
A much much simpler approach is to set the destination in the node form's URL. For example, if you opened http://example.com/node/add/mytype?destination=my_custom_destination
, you will be redirected to that URL instead of the node view page.
- Somehow it doesn't work for me :( . Every function is reached but redirection doesn't happen. Feb 10 '14 at 16:07
- It will not work if you add the submit handler in this way, you need to be sure you are adding the submit handler to the right place, and normally in node form thats under the action
$form['actions']['submit']['#submit'][]="handler_name"
– ZiftmanMar 29 '15 at 17:39
This works for me using Drupal 7, after creating a new module! Put into the .module file the following PHP code:
<?php
function custom_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == '[node-type]_node_form') {
$form['actions']['submit']['#submit'][]= 'my_custom_submit_handler';
}
}
function my_custom_submit_handler($form, &$form_state) {
$form_state['redirect'] = 'http://www.expamle.eu/?q=node/2';
}
You just need to change [node-type]_node_form with your node type name (e.g.: example_node_form) and the http://www.expamle.eu/?q=node/2 with the correct URL.