我正在尝试在删除内容之前执行一些代码。 Rules模块包含
的事件但是,这些都不是在合适的时间执行我的代码。
我发现了一个名为Predelete的简单模块,它提供了在删除前执行代码的钩子。这似乎是调用规则事件的绝佳候选人。
因此,我基于Predelete中包含的“predelete_field”示例模块创建了一个非常简单的模块。该文件夹名为“predelete_field”,位于“modules”文件夹中,包含以下文件:
1:predelete_field.info
core = "7.x"
dependencies[] = "rules"
dependencies[] = "list"
dependencies[] = "predelete"
description = "Example for the predelete module with a content type and a node"
name = "Predelete Field"
package = Other
project = "predelete_field"
version = "7.x-1.0"
; Information added by drupal.org packaging script on 2011-07-11
version = "7.x-1.1"
core = "7.x"
project = "predelete"
datestamp = "1310360219"
2:predelete_field.module
<?php
/**
* Implements hook_predelete_node().
*/
function predelete_field_predelete_node($node) {
drupal_set_message( "PREDELETE HOOK CALLED", 'warning' );
rules_invoke_event('predelete_field', $node);
$deletable = TRUE;
$reason = t('Deletable by default.');
return array('result' => $deletable, 'reason' => $reason);
}
3:predelete_field.rules.inc
<?php
/**
* Implements hook_rules_event_info() on behalf of the predelete_field module.
*/
function rules_predelete_field_event_info() {
$items = array(
'predelete_field_predelete' => array(
'label' => t('Before deleting content'),
'group' => t('Node'),
),
);
return $items;
}
不幸的是,这似乎不起作用:即使在清除缓存并禁用和重新启用模块之后,事件也不会显示在事件列表中。同样,drupal_set_message
函数似乎不会触发。
是否有人能够发现我可能犯的任何错误,或提供解决方案?
答案 0 :(得分:0)
Predelete模块似乎只是挂钩到多个节点删除的确认表单,它不会被使用规则触发,因为规则不会调用表单而是使用节点API。
如果查看node_delete_multiple()
function,虽然您会看到在从数据库中实际删除任何内容之前调用了几个钩子(即hook_node_delete
和hook_entity_delete
)。其中一个是你想在自定义模块中实现的钩子,如下所示:
function mymodule_node_delete($node) {
// Perform some action based on values in $the node object.
// Nothing has been deleted from the database at this point.
}
当您添加该钩子时,您需要再次清除Drupal的缓存,但根据文档它应该可以工作。