You are here

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

g089h515r806 的头像
Submitted by g089h515r806 on 星期五, 2009-07-31 11:50

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版本:

评论

thomasfan 的头像

function beep_action_info_alter(&$info) {
    if (isset($info['user_block_user_action']['hooks']['comment'])) {
        array_unique(array_merge($info['user_block_user_action']['hooks']['comment'],array('insert')));
    }
    else {
        $info['user_block_user_action']['hooks']['comment'] = array('insert');
    }
}
pangjanne 的头像

 array_unique()应该是不对的,首先这个函数的使用就没有正确,它只接受第二为int型的参数(而不是数组),作用书移除数组中重复的值;其次这里的逻辑是:如果该动作注册了commnet钩子的某些触发器,那么我们就把insert触发器添加进去,没有便注册一个;所以原著并没有错误,还是应该使用array_merge()函数;

 

试想一下如果user_block_user_action动作注册的hooks为array('commnet'=>array('update')),这时array_unique()函数什么也做不了(不可能使得该动作对insert触发器可用),同时php的语法也是错误的;

xingwg 的头像

勘误:

function beep_action_info_alter(&$info) {
  if (isset($info['user_block_user_action']['hooks']['comment'])) {
    $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');
  }
}