As i tested, after a node deletion, drupal always re-direct to the front page.

how can set the redirect destination after deletion in my module for various situation, like for blog, i want to redirect to the user's blog page, for forum i want to redirect to the forum page ...

thanks very much.

Comments

danielb’s picture

yourmodulename_delete(&$node) {
global $user;
if ($node->type == 'blog') {
drupal_goto("user/". $user->uid ."/blog");
}
if ($node->type == 'forum') {
drupal_goto("forums");
}
}

that's just a guess, I dont use blogs or forums so I have no clue, but the idea will be something like that

gnosis.kv’s picture

thanks danielb.

i think the following code should work

function yourmodule_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
  switch($op){
      case('delete'):
      if($node->type == "forum"){
        $forum_id = $node->tid;
        drupal_goto("forum/$forum_id"); //return to the forum topics list
      }elseif($node->type == "blog"){
        $uid = $node->uid;
        drupal_goto("blog/$uid"); //return to the author's blog
      }
    break;  
  }
}

cybershan’s picture

Mmmm....

I got the same problem, thanks for the code, but where should I paste this code to?

DeskRocket’s picture

You will need to create a custom module. Google 'Drupal custom module' or 'Drupal custom module tutorial'.

jaypan’s picture

There is a problem with that code above - if there are any modules that come after the module that has the code above, their delete functions will not be called, and there will be a bunch of data in the system that shouldn't be there. This could cause problems in the future.

I have used something like this:

my_module_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) 
{
  if($op == 'delete')
  {
    $_SESSION['redirect_delete'] = 'redirection/path';
  }
  if($op == 'load' && isset($_SESSION['redirect_delete'])
  {
    $destination = $_SESSION['redirect_delete'];
    unset($_SESSION['redirect_delete']);
    drupal_goto($destination);
  }
}

Since nodes will always be loaded on the front page (with the exception being when there are no nodes on the site after they have been deleted), 'load' will always be called after 'delete', and this process allows for all modules to run their hook_nodeapi with $op == 'delete'.

Contact me to contract me for D7 -> D10/11 migrations.

jannol’s picture

this will not work when user 1 wants to delete multiple

however a simple if ($GLOBALS['user']->uid != 1) {
will do the trick.

kambiz.aghaiepour’s picture

Any reason not to do the following right in the forum module ( in drupalroot/modules/forum/forum.module ):

Index: forum.module
===================================================================
--- forum.module (revision 43)
+++ forum.module (revision 71)
@@ -284,6 +284,7 @@

case 'delete':
db_query('DELETE FROM {forum} WHERE nid = %d', $node->nid);
+ drupal_goto("forum/$node->tid");
break;

case 'load':

I did this inside the function forum_nodeapi(), towards the bottom of the function and it seems to do exactly what I need (which is to return the forum user to the forum they were working in after a forum topic delete operation).

I realize this may break after an upgrade and will have to be added back in, so is there any way developers can add the above into the 6.x branch?

Kambiz

Note: The above is an SVN diff, and so the 1 line I added was the drupal_goto() call immediately after the db_query() in the case 'delete' clause, not including the "+" of course.

jaypan’s picture

Which hook is that in?

Contact me to contract me for D7 -> D10/11 migrations.

ey’s picture

This is a very BAD IDEA. You should never ever hack the core!
http://drupal.org/node/1559728

Old usernames: pc-wurm, Елин Й.

kevin.mcnamee@mailbox.org’s picture

This is very simple to do with the Workflow-ng module. For example, by default the user returns to the front page when they delete a node. The following workflow will redirect to the node parent if the node is part of a book hierarchy.

array (
  'cfg_13' => 
  array (
    '#type' => 'configuration',
    '#weight' => '0',
    '#event' => 'node_delete',
    '#label' => 'Redirect to book parent',
    '#id' => 1,
    '#altered' => false,
    '#module' => 'workflow-ng',
    '#active' => 1,
    0 => 
    array (
      '#name' => 'workflow_ng_condition_custom_php',
      '#id' => 2,
      '#type' => 'condition',
      '#label' => 'Node belongs to book',
      '#settings' => 
      array (
        'php' => 'return is_numeric([node:book_id]);
',
        'used_arguments' => 
        array (
          0 => 'node',
        ),
        'used_php_arguments' => 
        array (
        ),
      ),
    ),
    1 => 
    array (
      '#id' => 3,
      '#name' => 'workflow_ng_action_drupal_goto',
      '#type' => 'action',
      '#label' => 'Redirect to book parent',
      '#settings' => 
      array (
        'path' => 'node/[node:book_id]',
        'path_args' => 
        array (
          0 => 'node',
        ),
        'query' => '',
        'query_args' => 
        array (
        ),
        'fragment' => '',
        'fragment_args' => 
        array (
        ),
        'force' => 0,
        'override' => 1,
      ),
    ),
    '#name' => 'cfg_13',
  ),
)
norman.lol’s picture

<?php
/**
 * 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'ld like to redirect to
  $form_state['redirect'] = 'node/123';
}
?>