2012-01-12 48 views
1

我工作的模块,增加新的节点时,或编辑现有的节点时,使节点上的变化对时,Drupal的hook_nodeapi操作之前操作“更新”匹配“插入”添加新节点

但我发现,当添加新节点的hook_nodeapi操作情况下,“更新”和案例相匹配“插入”,假设匹配只有情况下,当“插入”

有什么办法做正确的方式,或区分两者之间“更新”案和“插入”案?

我使用Drupal的6

+0

你是否在你的'insert'钩子中调用了'node_save()'? – Clive 2012-01-12 16:09:38

+0

不,我不使用它, – 2012-01-12 21:24:03

+0

您是否已验证''更新'的情况下被调用与'插入'一个相同的节点?另外,你能告诉我们你的'hook_nodeapi()'实现的代码和从那里调用的函数吗? – 2012-01-13 11:08:28

回答

1

我想通了这个问题,这里是从drupal.org

<?php 
function hook_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) { 
    switch ($op) { 
    case 'presave': 
     if ($node->nid && $node->moderate) { 
     // Reset votes when node is updated: 
     $node->score = 0; 
     $node->users = ''; 
     $node->votes = 0; 
     } 
     break; 
    case 'insert': 
    case 'update': 
     if ($node->moderate && user_access('access submission queue')) { 
     drupal_set_message(t('The post is queued for approval')); 
     } 
     elseif ($node->moderate) { 
     drupal_set_message(t('The post is queued for approval. The editors will decide whether it should be published.')); 
     } 
     break; 
    case 'view': 
     $node->content['my_additional_field'] = array(
     '#value' => theme('mymodule_my_additional_field', $additional_field), 
     '#weight' => 10, 
    ); 
     break; 
    } 
} 
?> 

所以情况下插入和更新的情况下将hook_nodeapi被召集

0

当你想要采取行动时,你需要使用$ node-> type来区分。 现在您正在您网站的每个节点上进行操作。

if ($node->type == 'the_content_type_I_want') { 
    switch ($op) { 
    case 'presave': 
     break; 
    case 'insert': 
     break; 
    case 'update': 
     break; 
    case 'view': 
     break; 
    } 
}