1

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

Amir Iskander
  • 551
  • 1
  • 7
  • 21
  • Are you calling `node_save()` in your `insert` hook by any chance? – Clive Jan 12 '12 at 16:09
  • Have you verified that the 'update' case is invoked for the same node as the 'insert' one? Also, can you show us the code of your `hook_nodeapi()` implementation and the functions invoked from there? – Henrik Opel Jan 13 '12 at 11:08
  • Oh, and BTW: Be aware that both cases, 'insert' as well as 'update' are invoked _after_ the new or updated node has been saved. This is a common trap, as it is a bit counter intuitive. If you want to make changes _before_ the node gets saved, check out the 'presave' case, as this is invoked before. – Henrik Opel Jan 13 '12 at 11:17
  • Thanks guys, I made the code more general for the use in case "insert" and "update", and just used the "update", since it is called when adding nodes, and when editing nodes. – Amir Iskander Jan 13 '12 at 14:38

2 Answers2

1

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

Amir Iskander
  • 551
  • 1
  • 7
  • 21
0

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;
  }
}