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?

CommentFileSizeAuthor
#10 token_patch-207768-2.patch597 bytesroderik
#4 patch-207768.patch416 bytesjmstacey
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

derhasi’s picture

@@ +241,8 -241,8
$id = _token_get_id($type, $object);
-  if (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;
  }
greggles’s picture

Status: Needs review » Active

Could you provide this as a real patch http://drupal.org/patch/create ?

budda’s picture

Priority: Minor » Normal

I've just found a similar problem to this. token_get_values() is creating a static array based on $node->nid. However the token_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 :)

jmstacey’s picture

FileSize
416 bytes

This 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

budda’s picture

Status: Active » Reviewed & tested by the community

Patch (as simple as it is) works good for me.... +1

roderik’s picture

+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).

Summit’s picture

Subscribing, please commit this patch.
greetings, Martijn

greggles’s picture

Status: Reviewed & tested by the community » Needs work

This 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).

asak’s picture

I 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...? ;)

roderik’s picture

Status: Needs work » Needs review
FileSize
597 bytes

Thanks 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.

Cyberwolf’s picture

I 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.

Dave Reid’s picture

Status: Needs review » Fixed

Hey 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

Status: Fixed » Closed (fixed)

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