Prevent Drupal from Delete a node and add you own action

I had to prevent Drupal from deleting a node and instead run my own functions to act on "delete" operation. I used the form_alter hook and removed the delete button and added my own button called Remove. This don't prevent users from calling the node/[nid]/delete, you have to set that in the permissions instead.

<?php
function my_module_content_form_alter(&$form, &$form_state, $form_id) {
  if(
$form_id == "allocation_node_form") {
    if (isset(
$form['#node']->nid))  {
           
$form['buttons']['my_remove'] = array(
                                       
'#type' => 'submit',
                                       
'#value' => 'Remove',
                                       
'#weight' => 15,
                                       
'#submit' => array('allocation_remove_submit'),
                                        );
                                       
            if(
$user->uid != 1) {
              unset(
$form['buttons']['delete']);
             
$form['buttons']['#suffix'] = "<br>".t("<b>Remove</b> will...");
            }else{
             
$form['buttons']['#suffix'] = t("<b>Delete</b> only if ...");
            }
        }
  }

}


function
allocation_remove_submit($form, &$form_state) {
    if (
is_numeric($form_state['values']['field_a_team'][0]['nid'])) {
       
//my actions
       
        //Clear forms cache
       
$cid = 'content:'. $form_state['values']['nid'].':'. $form_state['values']['vid'];
       
cache_clear_all($cid, 'cache_content', TRUE);
       
       
//Redirect
       
drupal_goto("node/".$form_state['values']['field_a_team'][0]['nid']);       
    }else{
       
drupal_set_message(t("Need all values to be set"), "warning");
    }
}
?>
Knowledge keywords: