I'm using Auto Nodetitle vor getting a player-profile's title via first name and last name. For this node I also use imagefield to save a playerpicture. For beeing able to save images in subfolders via token, I modified imagefield (v2) with using $node instead of $user for token_replace.
Problem: When creating a new player-profile-node, an empty title is created.
Reason: The token replacement of imagefield allready creates $tokens['node']['']. Because of this $tokens['node'][''] isn't actualized when replacing the node title. But is has to get actualized, becaus some Information (like CCk-fields first_name, last_name) wasn't given before.
Possible solution:
with adding $id &&
to the if-clause, token is forced to get information from the actual node in every run on the node's creation ($id =''):
@@ +241,8 -241,8
$id = _token_get_id($type, $object);
- if ($id && isset($tokens[$type][$id])) {
+ if ($id && isset($tokens[$type][$id])) {//runs else if new node is in creation ($id = '')
$tmp_tokens = $tokens[$type][$id];
}
else {
$tmp_tokens = module_invoke_all('token_values', $type, $object, $options);
$tokens[$type][$id] = $tmp_tokens;
}
Now my problem is gone. But I didn't know why it only happened at node-creation, on node-update everything went well.
Could this addition cause trouble in other cases?
Comment | File | Size | Author |
---|---|---|---|
#10 | token_patch-207768-2.patch | 597 bytes | roderik |
#4 | patch-207768.patch | 416 bytes | jmstacey |
Comments
Comment #1
derhasi CreditAttribution: derhasi commentedComment #2
gregglesCould you provide this as a real patch http://drupal.org/patch/create ?
Comment #3
buddaI've just found a similar problem to this.
token_get_values()
is creating a static array based on$node->nid
. However thetoken_replace()
is being called on a $node object which has not yet been inserted in to the Drupal database - and so doesn't yet have a unique node id in the $node object.As mentioned above, doing a check to see if $node->nid exists before adding it to the $tokens array seems like a sensible idea.
As a txt I made the single line change to token.module (line 242) so that we now have
if ($id && isset($tokens[$type][$id])) {
.This makes token.module work as expected now.
Although this bug might be a niche issue, I still think its quite important to fix - so changed from minor to normal priority.
If you really really really need a patch creating for the sake of 6 characters I'll put it on the to do list :)
Comment #4
jmstacey CreditAttribution: jmstacey commentedThis simple fix appears to solve the problem when using the [nid] token in combination with imagefield. Without this patch you are required to create the node before uploading any pictures. In the (single) trial I ran after making the change derhasi provided everything seemed to work as expected.
http://drupal.org/node/288308
Comment #5
buddaPatch (as simple as it is) works good for me.... +1
Comment #6
roderik+1 & 'me too!'
I need it for http://drupal.org/node/288308 too, and have done several trials, actually (while amending patch for that issue).
Comment #7
Summit CreditAttribution: Summit commentedSubscribing, please commit this patch.
greetings, Martijn
Comment #8
gregglesThis will cause an e_all problem. I know 5.x doesn't care about that, but I'd rather we fix those before they are created.
We need an isset($id) && $id or maybe just the isset($id).
Comment #9
asak CreditAttribution: asak commentedI don't know what an e_all problem is - but is it a kind of problem that should stop me from using this on production even if it solves me some major issues...? ;)
Comment #10
roderikThanks for spotting greggles, and I guess it's understandable that you don't fix this stuff yourself...
I re-rolled and re-tested it. Changed stuff too. Since the $tmp_tokens is not used outside this construct, the assignment in the 'if' clause was useless so I deleted it.
Comment #12
Cyberwolf CreditAttribution: Cyberwolf commentedI encountered a similar problem with the combination of auto_nodetitle and csm modules. I was unable to use any tokens from CCK fields in the automatic title of the node, because of the internal cache. I think the best way to solve this is changing _token_get_id(), so it does not just use $object->nid as key for the static cache. The default case there, crc32(serialize($object)), looks much more interesting.
Comment #13
Dave ReidHey all, I simply backported the current code from the D6 version, which was just a simple check if $id was non-empty. This is now fixed in CVS.
http://drupal.org/cvs?commit=375784