by 蘇德宙, 2011-04-30 10:40, 人氣(1406)
1. includes/actions.inc
The actions engine, which executes actions
The actions engine, which executes actions
2. modules/trigger.module
The dispatcher for actions
The dispatcher for actions
3. modules/system.module
The configuration screens for adding, removing, and configuring individual actions
The configuration screens for adding, removing, and configuring individual actions
4. modules/trigger.module
The interface for assigning actions to events (that is, hooks)
The interface for assigning actions to events (that is, hooks)
5. individual modules
The hook that describes actions (hook_actions_info()) and the actions themselves live in
Ex. Actions that affect nodes, like the "Publish node" action, live in node.module
The hook that describes actions (hook_actions_info()) and the actions themselves live in
Ex. Actions that affect nodes, like the "Publish node" action, live in node.module
by 蘇德宙, 2011-04-30 11:03, 人氣(2038)
1. hook_action_info() - describe the action to Drupal
function user_action_info()
return array(
'user_block_user_action' => array(
'description' => t('Block current user'),
'type' => 'user',
'configurable' => FALSE,
'hooks' => array(
'nodeapi' => array('presave', 'delete', 'insert', 'update', 'view'),
'comment' => array('view', 'insert', 'update', 'delete'),
'user' => array('logout'),
),
),
return array(
'user_block_user_action' => array(
'description' => t('Block current user'),
'type' => 'user',
'configurable' => FALSE,
'hooks' => array(
'nodeapi' => array('presave', 'delete', 'insert', 'update', 'view'),
'comment' => array('view', 'insert', 'update', 'delete'),
'user' => array('logout'),
),
),
'user_block_ip_action' => array(
...
),
);
}
2. 'user_block_user_action'
function name of the action (key)
modulename + description of what the function does + '_action'
user + block user + action
2. Writing an action
function user_block_user_action(&$object, $context = array()) {
// get the uid from the object
if (isset($object->uid)) {
$uid = $object->uid;
}
elseif (isset($context['uid'])) {
$uid = $context['uid'];
}
else {
global $user;
$uid = $user->uid;
}
// make sure we have a user record
if ($uid) {
$user = user_load($uid);
// block the user
db_query("UPDATE {users} SET status = 0 WHERE uid = %d", $uid);
// log out the user
sess_destroy_uid($uid);
// record a message noting the action taken
watchdog('action', 'Blocked user %name / uid=%uid.', array('%name' => check_plain($user->name), '%uid' => $uid));
}
}
?>
// get the uid from the object
if (isset($object->uid)) {
$uid = $object->uid;
}
elseif (isset($context['uid'])) {
$uid = $context['uid'];
}
else {
global $user;
$uid = $user->uid;
}
// make sure we have a user record
if ($uid) {
$user = user_load($uid);
// block the user
db_query("UPDATE {users} SET status = 0 WHERE uid = %d", $uid);
// log out the user
sess_destroy_uid($uid);
// record a message noting the action taken
watchdog('action', 'Blocked user %name / uid=%uid.', array('%name' => check_plain($user->name), '%uid' => $uid));
}
}
?>