使用drupal_alter()修改已有的动作

Drupal运行action_info钩子时,每个模块都可以声明它所提供动作,Drupal还给了模块一个机会,让它们修改该信息----包括其它模块提供的信息。下面让我们修改“阻止当前用户”这个动作,让它可以用于评论插入这个触发器:

 
/**
* Implementation of hook_drupal_alter(). Called by Drupal after
* hook_action_info() so modules may modify the action_info array.
*
* @param array $info
* The result of calling hook_action_info() on all modules.
*/
function beep_action_info_alter(&$info) {
    // Make the "Block current user" action available to the
    // comment insert trigger. If other modules have modified the
    // array already, we don't stomp on their changes; we just make sure
    // the 'insert' operation is present. Otherwise, we assign the
    // 'insert' operation.
    if (isset($info['user_block_user_action']['hooks']['comment'])) {
        array_merge($info['user_block_user_action']['hooks']['comment'],
            array('insert'));
    }
    else {
        $info['user_block_user_action']['hooks']['comment'] = array('insert');
    }
}
 
    最终的结果就是,“阻止当前用户”这个动作现在可被分配了,如图3-5所示。
3-5.将动作“阻止当前用户”分配给评论插入触发器
 老葛的Drupal培训班 Think in Drupal

Drupal版本: