我正在寫一個自定義模塊,我想在節點被刪除之前做一些檢查。是否有鉤子在之前被刪除??有沒有辦法以某種方式防止刪除?順便說一下,我正在使用drupal6掛鉤之前調用刪除節點
回答
您可以使用hook_nodeapi
刪除。
它可能是一個壞主意,試圖阻止一個節點的缺失,因爲你不知道什麼是其他模塊都做了,如刪除CCK字段值等
沒有鉤子,你可以用它來在節點被刪除之前執行操作。以上是最接近你可以來。
在節點被刪除之前沒有被調用的鉤子,但Drupal會檢查node_access以查看是否允許用戶在繼續刪除之前刪除該節點。
您可以將節點訪問權限設置爲不允許用戶刪除節點:如果用戶是用戶1或具有管理節點權限,則不會有所幫助,因此不要將這些權限授予不受信任的用戶即將刪除節點的人)。這也是防止無端節點刪除的Drupal方式。
您可以使用hook_menu_alter將菜單回調node/%node/delete
指向您自己的功能。你的功能可以做任何你想要的檢查,然後出示node_delete_confirm
表格,如果檢查通過。
它適用於我的情況,因爲它是一個封閉的系統,但它存在安全缺陷,但對我的情況無關緊要。 – 2010-07-26 09:45:03
這不會保護使用視圖批量操作刪除節點。 – 2014-01-15 10:26:12
你可以使用hook_access並且在op == delete的時候放置條件。如果滿足條件fullfilled返回True否則返回false。如果錯誤,您的節點將不會被刪除。
記住管理員這將不會被觸發。
使用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']);
}
}
}
這將刪除刪除按鈕並添加您自己的按鈕和操作。這不會阻止用戶使用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");
}
}
該自定義模塊的代碼是爲Drupal 7,但我敢肯定,類似的概念也適用於Drupal的6.加,現在,你最有可能尋找爲Drupal 7
的解決方案此代碼將在「節點」被刪除之前運行,因此您可以運行所需的檢查,然後可以隱藏刪除按鈕以防止節點被刪除。查看函數的註釋以獲取更多信息。
這是截圖展示了最終結果:
這是所使用的自定義代碼:
<?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。它具有關於整個過程的詳細信息,並鏈接到資源,包括如何輕鬆創建自定義模塊,您可以在其中複製/粘貼上述代碼以使其工作。
祝你好運。
- 1. 在刪除wordpress文件之前掛鉤
- 2. 節點更新掛鉤Cron
- 3. 續集刪除掛鉤?
- 4. 掛鉤新/刪除C++
- 5. 在$ .post()之前掛鉤其他數據所有調用
- 6. 節點 - 紅色節點刪除回調
- 7. SetParent掛鉤,你怎麼解除掛鉤?
- 8. 在刷新頁面之前掛鉤
- 9. 黃瓜背景與掛鉤之前
- 10. WordPress的URI掛鉤「之前後搜索」
- 11. 在任務掛鉤之前耙動
- 12. 如何刪除Rspec之前(:all)掛鉤留下的測試數據?
- 13. 文件之前刪除其使用節點JS
- 14. 帶有HtmlUnit節點的WebDriver Grid掛鉤節點CPU
- 15. 在迴環之前執行回調'保存'掛鉤
- 16. 調用在刪除之前觸發
- 17. Cukespace掛鉤用於場景之前/之後?
- 18. WW_MOUSE_LL掛鉤不被調用
- 19. 掛鉤LoadLibrary API調用
- 20. ntdll.dll調用掛鉤問題
- 21. Woocommerce命令刪除掛鉤回調不被稱爲
- 22. Prestashop調試掛鉤
- 23. Git部署掛鉤不刪除已刪除的文件
- 24. 刪除節點
- 25. 刪除節點
- 26. 刪除節點
- 27. 在刪除jstree節點之前顯示確認對話框
- 28. 節點在重啓之前如何刪除pid文件?
- 29. React樹 - 如何刪除當前節點?
- 30. Drupal從節點刪除節點引用
重點在於我希望在節點上發生任何刪除之前進行訪問。我非常瞭解這個鉤子,但並不能幫助我,因爲我可以看到它 – 2010-07-25 11:31:29