How can I redirect a Drupal user after they create

2020-08-05 10:32发布

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!

2条回答
The star\"
2楼-- · 2020-08-05 10:44

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.

查看更多
Luminary・发光体
3楼-- · 2020-08-05 10:57

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.

查看更多
登录 后发表回答