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?? :)
| Comment | File | Size | Author |
|---|---|---|---|
| auto_nodetitle_2.patch | 669 bytes | lbh |
Comments
Comment #1
robloachYou could use the db_next_id() function.
Comment #2
colanThat 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.
Comment #3
robloachI 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.
Comment #4
colanActually, 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:
Is there some way the naming hook can be called later in the process, after there's a real nid perhaps?
Comment #5
lbh commentedSo many functions to use! I'll figure this stuff out, eventually... :)
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.
Comment #6
fagoActually, 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.
Comment #7
joachim commentedI'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
Comment #8
arski commentedinteresting, 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