我正在尝试以编程方式添加一些规则,我正在按照本教程根据规则管理不同的价目表。要创建规则,它使用 default_rules_configuration 钩子,该钩子将在“加载规则时”执行。
1 - 不是很清楚,当“正在加载规则”时,显然运行 cron 来做到这一点。这是触发它的唯一方法吗?
2 - 有没有办法以编程方式添加规则,所以可以在插入角色挂钩中添加规则,或者这个 default_rules 挂钩是唯一的方法吗?
谢谢
我正在尝试以编程方式添加一些规则,我正在按照本教程根据规则管理不同的价目表。要创建规则,它使用 default_rules_configuration 钩子,该钩子将在“加载规则时”执行。
1 - 不是很清楚,当“正在加载规则”时,显然运行 cron 来做到这一点。这是触发它的唯一方法吗?
2 - 有没有办法以编程方式添加规则,所以可以在插入角色挂钩中添加规则,或者这个 default_rules 挂钩是唯一的方法吗?
谢谢
1 - According to hook_default_rules_configuration
documentation:
This hook is invoked when rules configurations are loaded.
The function is actually called when you clear your cache as this is when Drupal rebuilds the default entities provided in code through entity_defaults_rebuild
.
You can examine the full call stack as to how hook_default_rules_configuration
function is called using debug_backtrace
2 - To set a rule that reacts on inserting a role, you actually have to create a rule that reacts on a user insert action and then check the role saved to see if it matches the role that you're interested in reacting to.
I find it easier to do this via the UI. Here's an export of a rule that checks to see if the user is assigned the anonymous role and sends an email to admin if so:
{ "rules_role_change_rule" : {
"LABEL" : "Role change rule",
"PLUGIN" : "reaction rule",
"REQUIRES" : [ "rules" ],
"ON" : [ "user_insert" ],
"IF" : [
{ "user_has_role" : { "account" : [ "account" ], "roles" : { "value" : { "1" : "1" } } } }
],
"DO" : [
{ "mail" : {
"to" : "admin@website.com",
"subject" : "User role changed",
"message" : "User role has changed",
"from" : "drupal@website.com",
"language" : [ "" ]
}
}
]
}
}
You would still have to implement hook_default_rules_configuration
but replace the rule in the tutorial with one that suits your needs.