I have the following entered into the input field for the auto_nodetitle of a specific content type:

Example | ABC 1234567- print str_pad($node->nid, 6, "0", STR_PAD_LEFT);

The php is being evaluated because I end up with a title of 'Example | ABC 1234567-000000' but the $node->nid variable is obviously not populated when the auto node title code is running. Is there any way to get around this limitation?

Thanks!

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

SpikeX’s picture

It's only when you first save a node. If you go back and edit the node, and then re-save it, the value will work. I suspect this has something to do with the "nid" value not being accessible when a node is first created.

christopherreay’s picture

FileSize
1.11 KB

I have created a patch that "fixes" this.

The patch is dirty, and evil. It causes the Node to be saved twice on the initial save, much like it is suggested that one does.
As it looks like, Spike you are doing something similar to me (in fact your php is almost exactly the same, looks like an invoice reference to me) then you might want to try this patch. The patch also "works" with pathauto, which is nice.

THIS PATCH IS NOT WELL THOUGHT OUT. THOUGH IT WORKS FOR ME, IT MIGHT CAUSE SOME WWIIEERRDD SIDE EFFECTS. So be careful with it and check its not doing wierd things

Oh, the patch is for the 6.x-1.2 version of this module.

christopherreay’s picture

Issue tags: ++patch

oh, some tags...

inforeto’s picture

Is insert run before or after the original save?
Is there any kind of post save op to the nodeapi hook?

cliffordx’s picture

subscribe! I used nid for my auto node title by it does not show up the nid. I need to edit and save it again in order to work. This doesn't happen when I first install auto_nodetitle. I suspect it's conflicting with either of these plugin that I later added:

field_permissions
locationmap
image_url_formatter
token (release update)

seddonym’s picture

This is how I got around the issue in Drupal 7:

/**
 * Implements hook_node_insert().
 */
function MYMODULE_node_insert($node) {
  //The following two lines allow the node's title to use the nid,
  //since there is a bug with auto_nodetitle().
  //First, it regenerates the title.
  //Then it uses _node_save_revision() to directly update the
  //node in the database without triggering any hooks (which would
  //result in an infinite loop).
  auto_nodetitle_set_title($node);
  _node_save_revision($node, $GLOBALS['user']->uid, 'vid'); 
}
nicholas.alipaz’s picture

Version: 6.x-1.2 » 6.x-1.x-dev
FileSize
693 bytes

I have written a patch for this. The issue is that the $node->nid is not yet created when saving a node for the first time, however in all other saves the $node->nid is available. To bypass the issue there is really only one solution that I can think of and that is to:

If the nid is NOT set then we wait to do our auto_nodetitle_set_title($node); since this means we are in the initial creation of a node.
Instead we will wait until the insert $op where the $node->nid will be set and then we do auto_nodetitle_set_title($node); then resave the node.

This means the very first time that we save the node that we actually do two saves, which is not very resource friendly, but it is the only real solution.

The only thing I might do to make this patch better is to add a checkbox setting in the content type that says something like, "I have used the $node->nid or a token that is dependant on the node nid in my title pattern." When checking the box we would do the double save, otherwise we could just do everything in presave as the module currently does.

nicholas.alipaz’s picture

Status: Active » Needs review
nicholas.alipaz’s picture

Here is a version of the patch with the additional setting for turning on the setting per content type as I described in #7.

nicholas.alipaz’s picture

oops, logic was backwards in the presave op of hook_nodeapi. Fixed and new patch attached.

nicholas.alipaz’s picture

okay, one last pass at this. I realised one more needed condition to the logic and wanted to provide some documentation.

mvc’s picture

manish-31’s picture

Issue summary: View changes
Status: Needs review » Closed (outdated)

Stopped 6.x-1.x-dev development.