user2708473
user2708473

Reputation: 13

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.

Upvotes: 1

Views: 2239

Answers (2)

norman.lol
norman.lol

Reputation: 5374

Just for clarification I repeat the entire correct answer:

/**
 * Implements hook_form_alter()
 */
function MYMODULE_form_alter(&$form, &$form_state, $form_id){
  switch ($form_id) {
    case 'node_delete_confirm':
      // replace 'article' in next line with your node type machine name
      if($form['#node']->type == 'article') {
        $form['actions']['submit']['#submit'][] = 'node_delete_confirm_submit';
        $form['actions']['submit']['#submit'][] = '_MYMODULE_redirect';
      }
    break;
  }
}

function _MYODULE_redirect($form, &$form_state){
  // replace 'node/123' in next line with node you like redirect to
  $form_state['redirect'] = 'node/123';
}

Doing only $form['actions']['submit']['#submit'][] = '_MYMODULE_redirect'; was not enough. Still $form['actions']['submit']['#submit'][] = 'node_delete_confirm_submit'; needed to be triggered. Now the delete and the redirect are both triggered.

Upvotes: 0

EJK
EJK

Reputation: 326

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.

Upvotes: 1

Related Questions