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.
Comments
Comment #1
dave reidComment #2
dave reidComment #3
Anonymous (not verified) commentedWorks as advertised. Thanks.
Comment #4
gregglesThis 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.
Comment #5
dave reidForcing modules to assign themselves weights and clear token caches is a lot more difficult that adding an extra field from an existing db query. :)
Comment #6
dave reidAny progress on this issue?
Comment #7
gregglesThanks, 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