Hi

I am generating title based on cck text field using it's token.
when I store node, title is generated correctly.
But after it, when I edit node and I change my cck text field to e.g. "AAA", title is not affected at all.
I have to edit once more and then when I store node again, then it is changed. But if I changed it to another value e.g. "BBB", then it is changed to "AAA" (previous value)

thanks
Igor

Comments

igorik’s picture

when I use evaluate php code with print field[0]['value'] instead token, it works nice.

abaddon’s picture

ditto, -raw or -formatted perform the same, your php code workaround worked for me too

AdrianB’s picture

I was hit by this as well. Is this expected behavior or a bug?

nhck’s picture

The php code above should be something like:

print $node->field_name[0]['value']; //field_name should be replaced with your token name, leaving out the "-raw" or "-formatted"

Other than that I don't believe that it should be this way?

hefox’s picture

Title: token doesn't refresh on first node edit » Outdated titles when token_replace called on old node information before ANT

Form is rebuild on submit (normal drupal behaviour)

Pathauto calls token_replace on old node object.

token_get_values called by token_replace caches the node's token with old node information

ANT calls token_replace and old values are used.

Either gotta manually call token_get_values to get updated values (Has a flush, but can't pass it through token_replace) etc. or patch both modules, to get flush from token_replace or maybe it store by hash instead of object id, hm).

For now I'm just going to be using php :(.

hefox’s picture

function token_replace($original, $type = 'global', $object = NULL, $leading = TOKEN_PREFIX, $trailing = TOKEN_SUFFIX, $options = array(), $flush = FALSE) {

token_replace dev already has flush, so ant would just need to add that I guess. But, it would be nice if token_replace used the static cache based on a hash of the object, but that'd likely have performance issues.

edit: #855142: Better Handling of whether to use cached version of token values stopped over in token to see if they had an opinion.

Gerald Mengisen’s picture

An addition to comment #4:
If you need to have a term name of a taxonomy from a CCK taxonomy field to show up in the auto node title, then the following code could be useful:

$tid = $node->field_name[0]['value'];
$term = taxonomy_get_term($tid);
return $term->name;

The CCK field holds the tid, not the terms string representation. Not sure if this is the right way to do it, but it works for me and does not require the node to be saved twice.

Gerald Mengisen’s picture

@hefox

Some testing revealed that it is quite an unlikely candidate that breaks the ANT functionality: Pathauto.

I had a Drupal installation where ANT was working just fine, and after a "shotgun" update with Drush, I had the same problem as described here. So I went back to a backup and started updating modules one by one. It's not ANT, it's not Token, it's not CCK, not Flag, etc. But once I upgraded Pathauto from 6.x-1.1 to 6.x-1.3, ANT started showing the behavior described in this thread. So I guess another workaround would be to downgrade to Pathauto 1.1 since there were no database updates. BTW, further testing showed that the functionality broke between Pathauto 6.x-1.1 and 6.x-1.2.

I wonder if a new issue should be created for the Pathauto issue queue?

hefox’s picture

Oh, I should have mentioned that it was pathauto doing it. What is happening is that in the form rebuild, pathauto is calcaulting what would have been the path based on the old node (which is how it determines if the path has been overridden or should be automatically recalculated). However, it doesn't have to be pathauto, anything else in a form build that calls token_replace, would cause similar behavior. Hm, though I guess pathauto could check if the form has been submitted, and clear token's cache when so, but it doesn't seem like it's really pathauto's fault.

BTW, debug_backtrace() is excellent for finding out what is happening.

Gerald Mengisen’s picture

Hmm, I might not fully understand. The version 1.1 of Pathauto did not have that problem, and then something changed that broke ANT. If that change would be reversed and done in a different way (hopefully there is one), the problem would be fixed, would it not?

hefox’s picture

Pathauto 1.1 is 2 years old and had an annoying issue where it did not remember that one has overriden the path so kept having to change the path. The change that caused this fixes that (really annoying) bug to my understanding.

The possible solutions I could see are
1) Pathauto clears cache after done; however, if any other module calls token replace on the old node, they would also have to know to clear the cache.
2) Ant clears the cache; however, this will still be a problem for modules if say, using filefield_paths without ant, so that doesn't seem like a very sustainable.
3) Token has more dynamic way of telling if it's tokens are old or not, which while I have some ideas, none that don't have some performance costs.

3 Seems unlikely, 2 would have to be done for any module doing token_replace during node_save , and 1 would be a patch to pathauto.

BTW, it should also only be happening on forms that do not have filefields, can someone confirm this also? (filefields set the form to be cached).

_paul_meta’s picture

i can confirm that nodes which do not have file fields still had the issue with outdated titles being used, and needing to be edited&saved twice still occurred.

_paul_meta’s picture

Also, can anyone show how to get php to output a node reference title ?

I have a few situations requiring this but don't know the correct syntax.

Thanks!

hefox’s picture

This is really the place for those questions, as it's a bug report not a support request and asking support questions bring the issue off track (and bump the issue for everyone subscribed). It would be better to ask on the forms or IRC

  db_result(db_query("SELECT title from {node} where nid=%d", $node->field_whatever[0]['nid']));
_paul_meta’s picture

thanks for your response. i guess i thought the question was relevant to people looking for a workaround to this bug. but i take your point :)

liquidcms’s picture

re: #4 above: i think return not print.

fago’s picture

Status: Active » Closed (duplicate)