Use NID as node title
gavin_s - November 21, 2007 - 16:13
| Project: | Automatic Nodetitles |
| Version: | 5.x-1.1 |
| Component: | Miscellaneous |
| Category: | support request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | closed |
Jump to:
Description
I need to set the node title to the nid. I assume I need some php code to do this? Or is it not possible as the node will not have an nid until it is saved?
Thank you

#1
hmm...similar thing happens with [author-name] on first registration:
cannot assign title because user name is not saved yet...any thought on this much apreciated!
thx
#2
You're right (node-based) tokens are not available to use in autonodetitle when you create a new node, because, well, the node which they reference doesn't exist yet... however, you can reference the values which have been submitted in the form itself though.
To get the author's name, edit your content type settings for Automatic title generation to make sure that 'evaluate PHP in pattern' is checked, and then try this:
<?phpglobal $form_values;
$title = 'This is a new node written by '. $form_values['name'];
return $title;
?>
#3
As already said, It's not possible.
#4
Automatically closed -- issue fixed for two weeks with no activity.
#5
download:
http://drupal.org/project/auto_nodetitle
set "Automatically generate the title if the title field is left empty" and in "Pattern for the title:" write:
<?php$name = db_prefix_tables('{node}_nid');
$id = db_result(db_query("SELECT id FROM {sequences} WHERE name = '%s'", $name)) + 1;
return $id;
?>
#6
this is great, i added some code to get a random number
<?php$name = db_prefix_tables('{node}_nid');
$random = (rand()%1025);
$id = db_result(db_query("SELECT id FROM {sequences} WHERE name = '%s'", $name)) + $random * 3;
srand(time());
return $id;
?>
don't forget to check "Evaluate PHP in pattern"
#7
The problem with adding a random number is that you are not guaranteed a unique number for your title.
You could retrieve an ID of 50, for example, and add the randomly generated number 2 - producing 52; then, the next time you create content of that type, you could retrieve an ID of 51 and if the random number generated is 1, you end up with 52 again! That's why adding 1 every time will work better (you will have a unique number).
#8
I agree with you, but you can solve that with a bigger range of random numbers, if you generate a random number between 1 and 9, you will get the same numbers a lot, but if you generate a random number between 10,000 and 99,999, the possibility of getting the same number it's smaller (just math).
But that's not the biggest problem, after I submitted the last comment, I started to use a lot that code, and even when I only use the sequence query (without adding a random number), if I edit the node the Node Title changes because it's getting another value from the sequence table.
I have no idea of how to solve this.
Luis
#9
I've used this successfully in the past for getting around the [nid] issue:
<?php
//wrap [nid] in quotes or else we'll get a parse error
$token = '[nid]';
if (!empty($token)) {
// this node is not new
return [author-uid] . '-' . [type] . '-' . $token;
}
else {
// this node is new
$nid = db_next_id('{node}_nid');
return [author-uid] . '-' . [type] . '-' . $nid;
}
?>
#10
comment #5 will work but not when editing a node. If you edit an old node you will see that the node title is changed to the next sequential nid.
For the person commenting on #9, be careful if you use the function db_next_id() because this will increment the nid as well. You may not want this. And this will also not work if you edit an old node.
I have altered the code and found the following code to work and takes care of the case for editing old nodes as well:
<?php$token_nid = '[nid]';
$token_type = '[type]';
if (!empty($token_nid)) {
// this node is not new
return $token_type . ' - ' . $token_nid;
}
$name = db_prefix_tables('{node}_nid');
$id = db_result(db_query("SELECT id FROM {sequences} WHERE name = '%s'", $name)) + 1;
return $token_type . ' - ' . $id;
?>
#11
How would i do this if i wanted to use a taxonomy term which was only just chosen as the title for the node?
#12
Hmm, not sure but I am guessing that the tax. term will be stored in some variable at the time you submit (maybe $node). But do you really want to do this in the first place? what would happen if two users created a node with the same taxonomy term? That would mean two nodes with the same title.
Also what happens if a user chooses more than 1 tax. term?
#13
Yes, i do want to do this. Two users won't choose the same taxonomy term in this case. Only a few users will have the access to use this. It will be for "online instructors" to create a course title. They will not have any need to choose more than 1 tax term and i will set it so that this is not possible.
It works fine if the user saves the node and then goes into edit and saves again and i can live with this if need be, but if there is a way to get around this, it would make things much less complicated.
#14
#9's solution worked for me, adding +1 to the node ID when this is a new one.
<?php
//Define your Prefix:
$prefix='My-[type]-';
//wrap [nid] in quotes or else we'll get a parse error
$token = '[nid]';
if (!empty($token)) { // this node is not new:
return ("$prefix-$token");
}
else { // this node is new:
$nid = db_next_id('{node}_nid') +1;
return ("$prefix-$nid");
}
?>
#15
If your using MySQL here's the code that Drupal 5 uses for db_next_id():
<?php
function db_next_id($name) {
$name = db_prefix_tables($name);
db_query('LOCK TABLES {sequences} WRITE');
$id = db_result(db_query("SELECT id FROM {sequences} WHERE name = '%s'", $name)) + 1;
db_query("REPLACE INTO {sequences} VALUES ('%s', %d)", $name, $id);
db_query('UNLOCK TABLES');
return $id;
}
?>
If your using postgreSQL, it does the same thing, but uses the postgreSQL function nextval() to calculate the sequence.
So long as you have an else statement following the check for the existence of nid, you can safely use db_next_id() and not have to worry about incrementing anything, because you will definitely be dealing with new node, and your essentially assigning the nid the same way Drupal does.