Drupal's hook_nodeapi matches operation 'update' before operation 'insert' when adding a new node

679 Views Asked by At

I'm working on a module, that make changes on nodes when adding new node, or when editing an existing nodes,

but I have found that when adding a new node the hook_nodeapi's operation matches case "update" and case "insert", when it is assumed to match only case "insert"

Is there any way to do it the right way, or differentiate between the "update" case and "insert" case ?

I'm using Drupal 6

2

There are 2 best solutions below

0
On

I have figured out the problem, here is the hook_nodeapi from 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;
  }
}
?>

so for case insert and case update are called together

0
On

You need to use $node->type to distinguis when you want to act. Now you are acting on every node of your site.

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