Drupal 在删除节点之前调用钩子
我正在编写一个自定义模块,我想在删除节点之前进行一些检查。是否存在在删除节点之前触发的钩子?有没有办法防止删除?顺便说一句,我使用的是drupal6,您可以使用op delete 尝试停止删除节点可能不是一个好主意,因为您不知道其他模块做了什么,比如删除cck字段值等Drupal 在删除节点之前调用钩子,drupal,drupal-6,hook,drupal-modules,Drupal,Drupal 6,Hook,Drupal Modules,我正在编写一个自定义模块,我想在删除节点之前进行一些检查。是否存在在删除节点之前触发的钩子?有没有办法防止删除?顺便说一句,我使用的是drupal6,您可以使用op delete 尝试停止删除节点可能不是一个好主意,因为您不知道其他模块做了什么,比如删除cck字段值等 在删除节点之前,没有可用于执行操作的挂钩。上面是最接近您的地方。在删除节点之前没有调用钩子,但是Drupal会在继续删除之前检查node_访问,查看是否允许用户删除节点 您可以将节点访问权限设置为不允许用户删除节点:如果用户是用户
在删除节点之前,没有可用于执行操作的挂钩。上面是最接近您的地方。在删除节点之前没有调用钩子,但是Drupal会在继续删除之前检查node_访问,查看是否允许用户删除节点
您可以将节点访问权限设置为不允许用户删除节点:如果用户是用户1或具有“管理节点”权限,则不会有任何帮助,因此不要将这些权限授予不受信任的用户(即将删除节点的用户)。这也是Drupal防止不必要的节点删除的方法。您可以使用hook\u menu\u alter将菜单回调
node/%node/delete指向您自己的函数。您的函数可以执行任何您想要的检查,然后在检查通过时显示节点\u删除\u确认
表单。如果op==delete,您可以使用hook\u访问并放置条件。如果条件为fullfilled,则返回True,否则返回false。如果为false,则不会删除节点
请记住,对于管理员,这不会被触发。如果满足条件,请使用form\u 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");
}
}
function my_module_form_alter(&$form,&$form_state,$form_id){
if($form\u id==“分配\u节点\u表单”){
if(isset($form['#node']->nid)){
$form['buttons']['my_remove']=array(
“#键入”=>“提交”,
“#值”=>“删除”,
“#重量”=>15,
“#提交”=>array('allocation_remove_submit'),
);
如果($user->uid!=1){
取消设置($form['buttons']['delete']);
$form['buttons']['#suffix']=“
”.t(“删除遗嘱…”);
}否则{
$form['buttons']['#suffix']=t(“仅当…”时删除);
}
}
}
}
函数分配\删除\提交($form,&$form\状态){
如果(是数字($form_state['values']['field\u a\u team'][0]['nid'])){
//我的行为
//清除表单缓存
$cid='content:'.$form_state['values']['nid'].:'.$form_state['values']['vid'];
cache_clear_all($cid,'cache_content',TRUE);
//重定向
drupal_goto(“节点/”$form_state['values']['field_a_team'][0]['nid']);
}否则{
drupal_set_消息(t(“需要设置所有值”),“警告”);
}
}
此自定义模块代码适用于Drupal 7,但我确信类似的概念也适用于Drupal 6。另外,到目前为止,您很可能正在寻找Drupal7的解决方案
此代码将在删除节点之前运行,因此您可以运行所需的检查,然后选择性地隐藏删除按钮以防止删除节点。查看函数的注释以了解更多信息
这是显示最终结果的屏幕截图:
这是使用的自定义代码:
<?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']);
}
}
关键在于,我希望在节点上发生任何删除之前“访问”。我很清楚这个钩子,但在我看来它对我的案例没有帮助,因为它是一个封闭的系统,但它有一个安全缺陷,但对我的案例没有关系。这不会保护节点不被使用视图批量操作删除。