Hook called before deleting node

I am writing a custom module and I would like to do some checks before deleting the node. Is there a hook that gets trigerred up and the node is removed? And is there a way to somehow prevent deletion? BTW, I am using drupal6

+3
source share
7 answers

You can use hook_menu_alter to point the menu callback node/%node/deleteto your own function. Your function can perform any checks, and then submit the form node_delete_confirmif the checks pass.

+3
source

"" . URL/ node/[nid]/delete node, .

function my_module_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");
    }
}
+2

hook_nodeapi op delete.

, , node, , , cck ..

, node. , .

+1

form_alter , . - .

function xxx_contact_form_alter(&$form, $form_state, $form_id) {
  global $user;

  if (strstr($form_id, 'xxx_node_form')) {
    // Stop deletion of xxx users unless you are an admin
    if (($form['#node']->uid) == 0 && ($user->uid != 1)) {
      unset($form['actions']['delete']);
    }
  }
}
+1

Drupal 7, , Drupal 6. , , , Drupal 7.

"", node , , , , , node. .

, :

Screenshot showing node deletion prevention using custom code hook

:

<?php

/**
 * Implements hook_form_FORM_ID_alter() to conditionally prevent node deletion.
 * 
 * We check if the current node has child menu items and, if yes, we prevent
 * this node deletion and also show a message explaining the situation and 
 * links to the child nodes so that the user can easily delete them first
 * or move them to another parent menu item.
 * 
 * This can be useful in many cases especially if you count on the paths of 
 * the child items being derived from their parent item path, for example.
 */
function sk_form_node_delete_confirm_alter(&$form, $form_state) {
    //Check if we have a node id and stop if not
    if(empty($form['nid']['#value'])) {
        return;
    }

    //Load the node from the form
    $node = node_load($form['nid']['#value']);

    //Check if node properly loaded and stop if not
    //Empty checks for both $node being not empty and also for its property nid
    if(empty($node->nid)) {
        return;
    }

    //Get child menu items array for this node
    $children_nids = sk_get_all_menu_node_children_ids('node/' . $node->nid);
    $children_count = count($children_nids);

    //If we have children, do set a warning and disable delete button and such
    //so that this node cannot be deleted by the user.
    //Note: we are not 100% that this prevents the user from deleting it through
    //views bulk operations for example or by faking a post request, but for our
    //needs, this is adequate as we trust the editors on our websites.
    if(!empty($children_nids)) {
        //Construct explanatory message
        $msg = '';

        $t1 = '';
        $t1 .= '%title is part of a menu and has %count child menu items. ';
        $t1 .= 'If you delete it, the URL paths of its children will no longer work.';
        $msg .= '<p>';
        $msg .= t($t1, array('%title' => $node->title, '%count' => $children_count));
        $msg .= '</p>';

        $t2 = 'Please check the %count child menu items below and delete them first.';
        $msg .= '<p>';
        $msg .= t($t2, array('%count' => $children_count));
        $msg .= '</p>';

        $msg .= '<ol>';        
        $children_nodes = node_load_multiple($children_nids);
        if(!empty($children_nodes)) {
            foreach($children_nodes as $child_node) {
                if(!empty($child_node->nid)) {
                    $msg .= '<li>';
                    $msg .= '<a href="' . url('node/' . $child_node->nid) . '">';
                    $msg .= $child_node->title;
                    $msg .= '</a>';
                    $msg .= '</li>';
                }
            }
        }
        $msg .= '</ol>';

        //Set explanatory message
        $form['sk_children_exist_warning'] = array(
            '#markup' => $msg,
            '#weight' => -10,
        );

        //Remove the 'This action cannot be undone' message
        unset($form['description']);

        //Remove the delete button
        unset($form['actions']['submit']);
    }
}

node Drupal 7. , , , , / , .

.

+1

, , node , Drupal node_access, , node, .

node, node: , 1 , (.. , node). Drupal node .

0

hook_access , op == delete. True, false. false node .

, .

-1

Source: https://habr.com/ru/post/1756412/


All Articles