2

I am using the following function to alter the node's data before saving it to the database.

function mymodule_node_presave($node) {
  if ($node->type="mytype") {
    if ($node->is_new) {
      $node->status = 0;
    }
  }
}

How do I know that the node is new and not updated/edited? In the example, you may see I have used $node->is_new, and I need to make sure that is correct. So is it?

5
  • Surely the easiest way to find that out would be to try it?
    – Clive
    Commented Mar 27, 2013 at 10:16
  • @Clive yeah but i expect that a person like you with many years of experience with drupal would comment with yes or no and i'll take the answer for good. :)
    – Vlasin
    Commented Mar 27, 2013 at 10:18
  • can you check if the $node object exists yet? Commented Mar 27, 2013 at 10:23
  • rules module has 'event after saving content' you can try it.
    – Ali Nouman
    Commented Mar 27, 2013 at 10:23
  • @Vlasin Fair enough ;)
    – Clive
    Commented Mar 27, 2013 at 10:27

1 Answer 1

6

The is_new property is set by the node_save function, which is the same function that will invoke hook_node_presave - so your are safe in your assumption that this property will be present.

If you were not doing this in hook_node_presave, another way of testing if a node is new is by checking if the nid property is present and non-zero:

if (empty($node->nid)) {
  // ... this is a new node ...
}
1
  • 1
    ok , thanks , after some digging as Clive suggested i discovered the answer myself.
    – Vlasin
    Commented Mar 27, 2013 at 10:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.