2012-01-12 46 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; 
    } 
}