Search code examples
http-redirectdrupaldrupal-7

Redirect after node delete does not delete the node


I am implementing the 'form_alter' hook in Drupal 7. I want to redirect the web to a specific node after deleting any node of type 'article'. It seems that the proper way of doing so is:

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

       switch ($form_id){
          case 'node_delete_confirm':
          if($form['#node']->type == 'article'){
          $form['actions']['submit']['#submit'][] = '_mymodule_redirect';
          }
          break;  
        }

    }

    function _mymodule_redirect($form, &$form_state){

          $form_state['redirect'] = 'node/60';      

    }

When I put this code in my module it does redirect after confirming the node delete but the node is not actually deleted, if I go to the home page it is still alive!

If I remove the code the node is deleted as expected and the webpage is redirected to the frontpage as usual.

What am I doing wrong?

UPDATE: I forced the 'node_delete_confirm_submit' before the redirect action writing the following line before adding my redirect handler:

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

This solves the problem.


Solution

  • The easiest way to accomplish this task (and not have to use a hook at all) would be to use the Rules module. It's a nice clean way of performing any number of actions on your site, and I know there's a rule for redirecting the user after content of a certain type is deleted.