Community & Support

hook_nodeapi insert not working right (I think)

Before a node is inserted, I'm trying to change the title of it based on certain criteria.

So after checking that $op is "insert", I do my checks, and then have the following code:

<?php
$node
->title = 'Entry #' . $new_entry_count;
?>

But the title is not changed. Now, on the node view page immediately after creation, the status message says "Entry Entry #8467 has been created."

So that status message gets the change, but the it's still the original title in the database.

Is there another place I should do this change?

Thanks!

Comments

You want to use the case

You want to use the case where $op is "presave", something like

<?php
if  ( $op == 'presave'  && empty($node->nid) ) {
 
$node->title = 'Entry #' . $new_entry_count;
}
?>

You might also want to look at Automatic Nodetitles

Is presave only called once,

Is presave only called once, though?

I'm setting the title to something based on a value that is dynamic, so it only needs to do it once. Is presave called even when just updating?

Thanks!

extract from book mentioned

extract from book mentioned below:
nodeapi - presave - When either saving a new post or updating an existing post

That's why the

That's why the empty($node->nid) on insert it will be true, for updates it will be false.

Oh ok. So I can just check

Oh ok. So I can just check if the nid is empty.

On insert, there will be no nid?

On updates, it will exist?

Sweet. This is just what I needed to know. Thanks!

Maybe it's the presave op

Maybe it's the presave op that you want.
According to my Pro Drupal Development book, the save op is after saving a new post..
Good luck :)