I was doing some contrib testing today and had some exceptions happen when I used $this->drupalCreateNode(); After a lot of debugging, I found that $node->name is not defined when inserting a new node (using node_save()), but once the node is retrieved using node_load(), it is defined. Simple little code change in token_node.inc:

function node_token_values($type, $object = NULL, $options = array()) {
...

-     $account_mail = db_result(db_query("SELECT mail FROM {users} WHERE uid = %d", $node->uid));
+     $account = db_fetch_object(db_query("SELECT name, mail FROM {users} WHERE uid = %d", $node->uid));

...

-      $values['author-name']      = check_plain($node->name);
-      $values['author-name-raw']  = $node->name;
-      $values['author-mail']      = check_plain($account_mail);
-      $values['author-mail-raw']  = $account_mail;
+      $values['author-name']      = check_plain($account->name);
+      $values['author-name-raw']  = $account->name;
+      $values['author-mail']      = check_plain($account->mail);
+      $values['author-mail-raw']  = $account->mail;

Will assure that the token ['author-name'] will always be available. To test this, assign a token action on "After saving a new post." The token is not replaced as expected.

CommentFileSizeAuthor
#2 366370-undefined-node-name-D6.patch1.58 KBdave reid

Comments

dave reid’s picture

Title: $node->name is undefined when using nodeapi('insert') with DrupalWebTestCase » $node->name is undefined when using nodeapi('insert')
dave reid’s picture

Status: Active » Needs review
StatusFileSize
new1.58 KB
Anonymous’s picture

Status: Needs review » Reviewed & tested by the community

Works as advertised. Thanks.

greggles’s picture

This seems like a slight performance hit for the benefit of more accurate data. I'm +1 on it.

One note - Pathauto gets around this problem by assigning a heavier weight (greater than 0) and clearing the token cache when it gets the replace data. If a module that uses token can do that it could be a decent solution.

dave reid’s picture

Forcing modules to assign themselves weights and clear token caches is a lot more difficult that adding an extra field from an existing db query. :)

dave reid’s picture

Any progress on this issue?

greggles’s picture

Status: Reviewed & tested by the community » Fixed

Thanks, Dave Reid. Fixed in both 5.x and 6.x (the patch applied to 5.x and I feel pretty sure there were no schema changes...)

6.x http://drupal.org/cvs?commit=211634
5.x http://drupal.org/cvs?commit=211636

Status: Fixed » Closed (fixed)

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