知識社群登入
Create a new action
by 蘇德宙, 2011-04-30 11:03, 人氣(2041)
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'),
        ),
      ),

    '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));
  }
}
?>