Use NID as node title

gavin_s - November 21, 2007 - 16:13
Project:Automatic Nodetitles
Version:6.x-1.x-dev
Component:Code
Category:feature request
Priority:normal
Assigned:Unassigned
Status:needs review
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

marcoBauli - December 4, 2007 - 23:52

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

tanoshimi - December 9, 2007 - 21:03

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:

<?php
 
global $form_values;
 
$title = 'This is a new node written by '. $form_values['name'];
  return
$title;
?>

#3

fago - December 11, 2007 - 11:44
Status:active» fixed

As already said, It's not possible.

#4

Anonymous - December 25, 2007 - 11:51
Status:fixed» closed

Automatically closed -- issue fixed for two weeks with no activity.

#5

tifonapoli.net - January 28, 2008 - 16:11

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

lelizondob - January 30, 2008 - 19:03

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

mstalcup - March 13, 2008 - 21:25

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

lelizondob - March 15, 2008 - 08:37

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

bobmarchman - April 21, 2008 - 21:01

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

xamount - May 12, 2008 - 17:37

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

gracearoha - June 4, 2008 - 06:42

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

xamount - June 5, 2008 - 16:08

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

gracearoha - June 6, 2008 - 03:44

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

mbria - July 14, 2008 - 20:44

#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

bobmarchman - July 18, 2008 - 18:57

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.

#16

dagomar - October 2, 2008 - 16:06

Same issue. I don't like the offered solutions; In a heavy duty website titles may not actually correspond to the nid. That is, if many requests are made at the same time, db_next_id could return the same id for multiple nodes, unless i am mistaken.

On creation of a new node the nid doesn't exist. So why not make the system automatically edit the node after it has been safed?

Just a thought.

#17

dagomar - October 3, 2008 - 10:34
Status:closed» postponed (maintainer needs more info)

I am sorry to reopen this issue. I am however desperately in need of a solid solution here. As in my previous post, I am concerned that the offered solution may not provide the actual nid. In a heavy duty website with thousands of users, if many people are adding posts in the same time, I am concerned that #14 might provide the same number for multiple nodes. So I have a theoretical solution in mind, and i hope that someone could give me some thought on it.

When I think about using Token, I think about Pathauto. Using the nid in Pathauto seems to work fine. I think this is because pathauto writes the path to the database after the node has been saved, thus having no problem using the nid. As far as I can tell Automatic Nodetitles alters the Title of a node before saving the node, therefore making the use of nid for the creation of nodes impossible. This in my opinion is in conflict with what Automatic Nodetitles should be able to do, it's not really logical that it can use Token, but not nid, author and perhaps other Tokens. So I think the module should be changed to save the node title in a similar way that Pathauto does; just a simple "UPDATE" SQL after the node has been saved and all data is available from Token.

Any comments on this? Thanks.

#18

dagomar - October 3, 2008 - 11:37
Version:5.x-1.1» 6.x-1.0
Status:postponed (maintainer needs more info)» needs review

Hi again.
I hacked a possible solution. I don't know how to create a patch, so i am posting the code here. It is a nodeapi function, and it will only be called on creation of a new node. So basically a new node is saved 2 times. I put the code after auto_nodetitle_set_title, on line 74. Try it out and give me some feedback please!

/**
* Implementation of hook_nodeapi().
* Needed for newly saved nodes
*/
function auto_nodetitle_nodeapi(&$node, $op, $teaser, $page) {
// First check if this nodetype is auto_nodetitle enabled
$setting = auto_nodetitle_get_setting($node->type);
if ($setting == AUTO_NODETITLE_ENABLED || ($setting == AUTO_NODETITLE_OPTIONAL && empty($form_state['values']['title']))) {
switch ($op) {
// We only need to do this for new nodes. It will save the node again after creating the title
case 'insert':
$types = node_get_types();
$pattern = variable_get('ant_pattern_'. $node->type, '');
if (trim($pattern)) {
$node->changed = time();
$node->title = _auto_nodetitle_patternprocessor($pattern, $node);
}
else if ($node->nid) {
$node->title = t('@type @node-id', array('@type' => $types[$node->type]->name, '@node-id' => $node->nid));
}
else {
$node->title = t('@type', array('@type' => $types[$node->type]->name));
}
node_save($node);
break;
      default:
        break;
    }
  }
}

#19

dagomar - October 3, 2008 - 12:59

I revised my code a little bit. Now it will put the title in AFTER the node is being saved, regardless whether its new or updated. The code should go in auto_nodetitle.module around line 74. This should work for drupal 5, but this is untested. I am using drupal 6.4.

/**
* Implementation of hook_nodeapi().
* Needed for newly saved nodes
*/
function auto_nodetitle_nodeapi(&$node, $op, $teaser, $page) {
// First check if this nodetype is auto_nodetitle enabled
$setting = auto_nodetitle_get_setting($node->type);
if ($setting == AUTO_NODETITLE_ENABLED || ($setting == AUTO_NODETITLE_OPTIONAL && empty($form_state['values']['title']))) {
switch ($op) {
// Now works for insert and update. Title is stored in database with db_query instead of node_save
case 'insert':
case 'update':
$types = node_get_types();
$pattern = variable_get('ant_pattern_'. $node->type, '');
if (trim($pattern)) {
$node->changed = time();
$node->title = _auto_nodetitle_patternprocessor($pattern, $node);
}
else if ($node->nid) {
$node->title = t('@type @node-id', array('@type' => $types[$node->type]->name, '@node-id' => $node->nid));
}
else {
$node->title = t('@type', array('@type' => $types[$node->type]->name));
}
drupal_set_message($node->title . t(' has been generated. Node id is ') . $node->nid);
db_query("UPDATE {node} SET title = '%s' WHERE nid = %d", $node->title, $node->nid);
db_query("UPDATE {node_revisions} SET title = '%s' WHERE nid = %d AND vid = %d", $node->title, $node->nid, $node->vid);
break;
      default:
        break;
    }
  }
}

#20

asak - October 3, 2008 - 21:08

subscribing.

#21

tanjerine - October 24, 2008 - 12:28

subscribing

#22

tanjerine - October 24, 2008 - 12:56

this code seems to be working fine for me. no problems (yet). thank you for this.

#23

baobab - October 26, 2008 - 00:44

for Drupal 5 works ok.

#24

fago - October 28, 2008 - 14:32
Status:needs review» needs work

This code won't get into the official module - so I change the status.

#25

Starnox - December 1, 2008 - 17:26

Does #2 work in six? I can't seem to get it working. I need to get the value of the publish_on field from the scheduler module into the title.

Tried

<?php
 
global $form_values;
  return
$form_values['publish_on'];
?>

without any luck?

#26

poloparis - December 15, 2008 - 19:32

Great job Dagomar, thank you very much

quick question, how would I modify the code on #19 to remove the nodetype in front of the nid to get a title resembling "1234" instead of "Story 1234"

Thank you

#27

dagomar - December 19, 2008 - 15:27

I would imagine this can be done with the pattern you select?

For instance, i wanted the title of the node to be the nid, so i just used [nid] as title. This can be done on
"/admin/content/types/yourcontenttype" and then 'Automatic title generation'. I chose "Automatically generate the title and hide the title field" and as pattern "[nid]", like a said. If u have problems just PM me.

#28

cincy_kid - March 29, 2009 - 14:21

Is everyone using #19 as a solution for this or has there been anything else available that we can use to get the [nid] as the Title during creation in D6?

Susbscribing

#29

lelizondob - March 29, 2009 - 18:28

There's a lot of comments with code in this issue, most of them work. I know the logic way to do this would be just to add the token [nid] but that won't work without a patch, instead you have to use some php code, I usually use #14 without a problem.

#30

takinola - April 10, 2009 - 17:27

There's a simple workaround using the Rules (or Workflow-ng) module

See http://drupal.org/node/360359#comment-1460918

#31

aharown07 - April 24, 2009 - 02:35

If the patch in #19 works, what still needs work here? Not clear on why it can't be rolled into the module. I think dagomar's reasoning makes alot of sense.

#32

Parkes Design - April 27, 2009 - 13:25

takinola you are a legend... thanks for your solution. I agree with Aharown07 why hasn't this rolled into the module already?

#33

aharown07 - April 27, 2009 - 18:33

I'll add that I've been using patch19 for a few days now in test environment and have not seen any problems. (I don't really want to install Rules since I have no other use for it and it looks a bit heavy... that and I can't seem to understand how to make it work :) )

#34

Aren Cambre - April 28, 2009 - 03:11

subscribe

#35

Aren Cambre - April 28, 2009 - 03:12
Version:6.x-1.0» 6.x-1.x-dev
Component:Miscellaneous» Code
Category:support request» feature request
Status:needs work» needs review

Updating issue.

#36

ilakshmir - April 29, 2009 - 18:51

Just create a rule. Similar to the suggested solution in http://drupal.org/node/360359#comment-1460918, I use the following in the custom PHP code option for actions on "add a node":

$node->title = $node->nid;

This does not require automatic nodetitles.

#37

marcushenningsen - June 22, 2009 - 01:44

This is not working with the latest dev version, and I can't seem to get the code from #19 to work.

Is someone working on adding this to the module? Or could someone please tell me where to add the above mentioned code in either version? Thanks in advance.

#38

aharown07 - June 22, 2009 - 17:52

#19 is still working fine for me. You put it in auto_nodetitle.module. I don't remember now if I just added the code or replaced what was there but in my file it starts around line 61 and ends around 94.
I think it basically replaces the whole hook_nodeapi implementation part.

No idea why it hasn't been rolled into the module or made avail as an admin option or something.

#39

marcushenningsen - July 1, 2009 - 12:00

Thanks aharown07, I just replaced the hook_nodeapi part with the code, and it's working well.

#40

aharown07 - July 2, 2009 - 22:41

Good... now we both know! :) (... and I thought I'd documented all my hacks so well)

#41

gumdrop - July 3, 2009 - 15:28

Ok I applied #19. However if I set "Automatically generate the title if the title field is left empty" and enter or type a value or something in the Title field it ignores it and enters the content type an node id no. Or am I not understanding something? I thought it's supposed to use the entered value first?

 
 

Drupal is a registered trademark of Dries Buytaert.