i have code that programmatically creates nodes [178506#comment-1590834] using node_save. everything worked great until i upgraded automatic nodetitles to 1.2 which now hooks nodeapi presave. watching each hook presave get called i can see that automatic nodetitle reverts the title field of the new node back to whatever the title was for the first node saved. i searched for static or node_load operations within the autonodetitle to see if that was causing it, but i can't find anything that looks wrong. is there something i need to change in my code to be able to get bulk node_save operations to insert new nodes working with auto node title 1.2? i set the title by hand because when i wrote it, the autonode title only worked on nodes submitted via form. is that the culprit?

Members fund testing for the Drupal project. Drupal Association Learn more


dman’s picture

Version: 6.x-1.2 » 6.x-1.x-dev
Status: Active » Needs review
852 bytes

It is tokens fault - sort of.
I got this today.
Doing a bulk save of many nodes (a feeds consumer, that is populating staff profiles from a big list. node title is a concat of firstname,lastname)

Yes - huge numbers of nodes all get the previous values. This is due to static caching by token module.
token.module:token_get_values() will keep a list of nodes it has seen before, so it doesn't rebuild the values each time.
*problem* is that it indexes them by vid or nid.
Bulk new nodes have neither at presave time. They all get indexed as node 0. So all later requests get the values that were found first.
token.module could be a little more intelligent and NOT cache anything with nid=0, because that will seldom help.

Or we can do the following: use tokens 'flush' flag on nid=0 nodes.

function _auto_nodetitle_patternprocessor($output, $node) {
  if (module_exists('token')) {
    // If bulk processing many new nodes, they all have nid=0. 
    // This means we have to flush the token cache or _token_get_id() will 
    // think all nodes get the same values. Which is unlikely to be what we want.
    $flush = ($node->nid == 0) ? TRUE : FALSE;
    $output = token_replace($output, 'node', $node, TOKEN_PREFIX, TOKEN_SUFFIX, array(), $flush);
cybermache’s picture

I was hoping that this would be the fix for my situation but unfortunately my situation still exists. Sorry if this does not apply to this issue.

I am using Date Repeat Node Generator to have nodes created for each repeat date, instead of simple text listed on one node. I also have Signups enabled on this node type which is one reason I am using the Generator. I have the Automatic Nodetitle set to display the month/date in numerical format. The problem that is happening is they all get filled with the first nodes date field values. This only seems to be happening with Auto_Nodetitles and not URL auto_aliases. The logic offered in this ticket makes sense but perhaps my issue might be something that Token should tackle instead?

Any feedback/thoughts would be helpful.


dman’s picture

Yes, it's a shortcoming in token.
Probably a good idea to try and raise an issue there - I propose above that caching is skipped if the nid/vid index key is blank. Should be a safe improvement,

elliotttf’s picture

Status: Needs review » Reviewed & tested by the community
852 bytes

This works, but will throw warnings if the nid isn't set (i.e. on feeds imports). I've attached an updated patch that uses empty() rather than a value check.