How can I redirect a Drupal user after they create new content

*爱你&永不变心* 提交于 2019-12-06 16:00:02

问题


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!


回答1:


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['#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.




回答2:


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.



来源:https://stackoverflow.com/questions/17227302/how-can-i-redirect-a-drupal-user-after-they-create-new-content

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!