时间:2010-07-25 11:01:44

标签: drupal drupal-6 hook drupal-modules

7 个答案:

答案 0 :(得分:3)

答案 1 :(得分:2)

这将删除“删除”按钮并添加您自己的按钮和操作。这不会阻止用户使用URL / node / [nid] / delete删除节点,使用权限设置。

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 :(得分:1)

答案 3 :(得分: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']);
    }
  }
}

答案 4 :(得分:1)

这个自定义模块代码适用于Drupal 7,但我确信类似的概念适用于Drupal 6.此外,到目前为止,您很可能正在为Drupal 7寻找解决方案。

此代码将在“删除”节点之前运行,因此您可以运行所需的检查,然后可选择隐藏删除按钮以防止删除该节点。查看函数的注释以获取更多信息。

这是展示最终结果的屏幕截图:

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's 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']);
    }
}

有关详细信息,请查看有关conditionally preventing node deletion in Drupal 7的详细博客文章。它包含有关整个过程的详细信息以及资源链接,包括如何轻松创建自定义模块,您可以将上述代码复制/粘贴到其中以使其正常工作。

祝你好运。

答案 5 :(得分:0)

答案 6 :(得分:-1)

如果op == delete,你可以使用hook_access并输入条件。如果你条件fullfilled返回True否则返回false。如果为false,您的节点将不会被删除。

请记住,管理员不会触发此事。