There I was, minding my own business, while creating a node using automatic nodetitles in conjunction with the token module. "What's this?," I pondered as my [nid] token was not applied to the title, "where has the node id gone?!" It was at this point a voice echoed throughout my head, "of course this is true, a node which hasn't yet been created can't have a node id." Eureka! Even though that mysterious voice had a point, I knew I could fix that. So I looked at the automatic nodetitle module and told it, "See that last node id created? Add the number one to that and it should be right -- but only do it if the node id doesn't already have a number."

I'm not sure if this is useful, or whether I'm overlooking something. I'm also not the best coder out there, but I figured nobody hit me with a club for submitting this though. Maybe?? :)

CommentFileSizeAuthor
auto_nodetitle_2.patch669 byteslbh

Comments

robloach’s picture

Status: Needs review » Needs work

You could use the db_next_id() function.

colan’s picture

That should work, unless of course some other node grabs that nid first. I'm not sure how thread-safe this stuff is. The other option is to not allow nids here, but we may not have to be that careful.

robloach’s picture

I just tried it with db_next_id(), and it was using the corrent node ID, but then it pushed the node's ID up one after setting it. So, let's say you make a new node and the previous node was node 5. The next node would have the URL of 6, but the node ID would be 7. Very strange.

colan’s picture

Actually, that makes total sense. db_next_id() not only returns the next ID, it also increments it first. So every time you call it, you're going to get a new one.

You can read the sequences table directly and get the last ID + 1 (without incrementing it), but I think this would be database dependent, and having it work with all supported databases would be messy. It still wouldn't be thread-safe, by the way. Also, the implementation is changing in D6.

I took a closer look at the above patch, and there is an issue with it:

  • Grabbing the newest node ID is a bad idea. If there was a newer node that was deleted, the newly created node would take on that nid. If we're working in the spirit of auto-increment fields, this shouldn't happen. Every created node should have a unique nid. If not, you'll end up with problems. For example, references to the deleted node will now point to the newly created one; this is something you don't want.

Is there some way the naming hook can be called later in the process, after there's a real nid perhaps?

lbh’s picture

So many functions to use! I'll figure this stuff out, eventually... :)

Grabbing the newest node ID is a bad idea. If there was a newer node that was deleted, the newly created node would take on that nid. If we're working in the spirit of auto-increment fields, this shouldn't happen. Every created node should have a unique nid. If not, you'll end up with problems. For example, references to the deleted node will now point to the newly created one; this is something you don't want.

That's a very good point. I might not be the sharpest one in these parts, but I should've considered that. Perhaps you're right that the best approach would be to set the title after the node is actually created.

fago’s picture

Status: Needs work » Closed (won't fix)

Actually, this token can't work as newly created nodes have no node id until they are saved. So the only way would be a kind of hack to try to determine the id of the node - however this won't be reliable.

So I won't commit such a hack. If you prefer to use it, use the php code feature.

joachim’s picture

I've had a thought about how this could be made to work: #1415434: pre-book entity IDs when saving new ones. But not sure if it's total crack or not. Feedback appreciated! :D

arski’s picture

interesting, maybe re-open the issue and set it to 7x? The feature is still missing and it would be a great one to have..

Cheers