Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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!
Comment | File | Size | Author |
---|---|---|---|
#11 | fix-node-nid-replacements-1109840-11.patch | 3.32 KB | nicholas.alipaz |
#10 | fix-node-nid-replacements-1109840-10.patch | 2.86 KB | nicholas.alipaz |
#9 | fix-node-nid-replacements-1109840-9.patch | 2.86 KB | nicholas.alipaz |
#7 | fix-node-nid-replacements-1109840-7.patch | 693 bytes | nicholas.alipaz |
#2 | enable_nid.patch | 1.11 KB | christopherreay |
Comments
Comment #1
SpikeX CreditAttribution: SpikeX commentedIt'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.
Comment #2
christopherreay CreditAttribution: christopherreay commentedI 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.
Comment #3
christopherreay CreditAttribution: christopherreay commentedoh, some tags...
Comment #4
inforeto CreditAttribution: inforeto commentedIs insert run before or after the original save?
Is there any kind of post save op to the nodeapi hook?
Comment #5
cliffordx CreditAttribution: cliffordx commentedsubscribe! 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)
Comment #6
seddonym CreditAttribution: seddonym commentedThis is how I got around the issue in Drupal 7:
Comment #7
nicholas.alipaz CreditAttribution: nicholas.alipaz commentedI 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.
Comment #8
nicholas.alipaz CreditAttribution: nicholas.alipaz commentedComment #9
nicholas.alipaz CreditAttribution: nicholas.alipaz commentedHere is a version of the patch with the additional setting for turning on the setting per content type as I described in #7.
Comment #10
nicholas.alipaz CreditAttribution: nicholas.alipaz commentedoops, logic was backwards in the presave op of hook_nodeapi. Fixed and new patch attached.
Comment #11
nicholas.alipaz CreditAttribution: nicholas.alipaz commentedokay, one last pass at this. I realised one more needed condition to the logic and wanted to provide some documentation.
Comment #12
mvcsee also the related issues #823364: Add warning that nid cannot be used in node title and #194197: Use (T)NID as node title.
Comment #13
manish-31 CreditAttribution: manish-31 at OpenSense Labs for DrupalFit commentedStopped 6.x-1.x-dev development.