欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

How can I redirect a Drupal user after they create new content drupal7 d7 保存表单后 重定向 跳转 自己亲自做的 有大用 有大大用 有大大大用

Asked 
Active 3 years, 3 months ago    
Viewed 5k times
0
               

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!

                       
                                   
asked Jun 21 '13 at 3:35                                
                                   
                               
Add a comment                  
       

2 Answers 正确答案                    

ActiveOldestVotes                    
       
3
                   

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'; //有时用它正确

$form['actions']['submit']['#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.

                                       
answered Jun 21 '13 at 4:19                                    
                                       
                                   
  • Thanks for the quick response! Very helpful =) 
    – UpsetPopcorn                                        
     Jun 21 '13 at 13:47                                    
  • Somehow it doesn't work for me :( . Every function is reached but redirection doesn't happen. 
    – Łukasz Zaroda                                        
     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" 
    – Ziftman                                        
     Mar 29 '15 at 17:39                                     
Add a comment                    
       
0
                   

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.

                                       
answered Aug 24 '18 at 11:04                                    
                                       
                                   
Add a comment                    
       

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


           


           


               


       



Asked 
Viewed 184 times
2

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?

  • Yes, it is. But it is not a part of any module I know. Could you link module name in your question to it's project on drupal.org please? 
    – Mołot
     Jul 7 '15 at 14:22
  • What is the term page?  Jul 7 '15 at 14:35
  • Taxonomy has vocabulary, and each vocabulary has its own terms. What i meant is, If you display that particular term on a full view. 
    – Bhok
     Jul 7 '15 at 14:38
  • Using rules module you can redirect to other pages after the node has been saved. Try that module  Jul 7 '15 at 15:08

1 Answer 

 正确答案


0

You can set redirect state for your node form. For example

function my_module_form_alter( &$form, &$form_state, $form_id ){
  if($form_id == 'your_node_form_id') {
    $form['actions']['submit']['#submit'][] = '_mymodule_node_redirect'; 
  }
}

/**
 * Custom Submit Handler to set conditional redirection rule.
 */
function _mymodule_node_redirect($form, &$form_state) {
  // If terms are already in the system.
  if($form_state['values]['your_taxonomy_field']['und'][0]['tid'] ==  'tid_to_compare_with') {
    $form_state['redirect'] = 'whereever you want';
 }
}


来自 https://drupal.stackexchange.com/questions/164595/term-reference-redirect-after-node-save

  • Drupal 7's default behavior is to redirect the user to the full display of a node after it has been saved.

  • If you want to stay in the edit form after saving, add this snippet to a custom module adjusting the module namespace in the two functions.

  • For Drupal 8 or 9, have a look at the comment below by @zanvidmar.

  • Acknowledgements

  • mymodule.php


  • /**

    * Implements hook_form_BASE_FORM_ID_alter().

    */

    function mymodule_form_node_form_alter(&$form, &$form_state, $form_id) {

    $form['actions']['submit']['#submit'][] = '_mymodule_node_submit_redirect';

    }



    /**

    * Extra node form submit handler. Done this way to override the default.

    */

    function _mymodule_node_submit_redirect($form, &$form_state) {

    if (isset($_GET['destination'])) {

    unset($_GET['destination']);

    }

    $form_state['redirect'] = 'node/' . $form_state['nid'] . '/edit/';

    }



Thank you for the code snippet. I am not sure for which core version this applies. However since $form_state is an object, this code snippet should be updated to:

function _mymodule_node_submit_redirect($form, &$form_state) {
  if (isset($_GET['destination'])) {
    unset($_GET['destination']);
  }

  $url = \Drupal\Core\Url::fromRoute('entity.node.edit_form')
    ->setRouteParameters(['node'=> $form_state->getValue('nid')]);

  /** @var \Drupal\Core\Form\FormState $form_state  */
  $form_state->setRedirectUrl($url);
}


来自  https://gist.github.com/juampynr/660219418bde29b6d107



普通分类: