Today I experienced an annoying issue with combination of Automatic Nodetitles and Token modules.

  • Installed modules: Token 6.x-1.12 (weight set to 10); Rules 6.x-1.x-dev (weight set to 20); Workflow 6.x-1.x-dev (weight set to 0); Automatic Nodetitles 6.x-1.2 (weight set to 5).
  • Created Rule: Grand 100 Userpoints on changing Workflow state of content from "Pending" to "Approved" (Allowed states: Pending, Approved, Declined) (see attached image below rules_settings.jpg)
  • User creates content, content has Workflow step = Pending by default.
  • Administrator approves created content: changes workflow state from Pending to Approved

On saving content, on executing condition "Textual comparison", the current workflow step will be "Pending", BUT... it HAS to be "Approved" already. Why it is not changed? - See explanation below.

When clicking "Submit" button, when content is being saved, node module invokes hook_nodeapi:

function node_save(&$node) {
  // Let modules modify the node before it is saved to the database.
  node_invoke_nodeapi($node, 'presave');

And... Automatic Nodetitles module has the hook_nodeapi implementation and uses tokens. So regardless of Automatic Nodetitle weight (which is 1 by default) token (which has weight of 10) will be initialized during hook_nodeapi invoke and will CACHE all tokens, including Workflow states, which are not saved in database from Pending to Approved yet.

function auto_nodetitle_nodeapi(&$node, $op) {
  if ($op == 'presave' && auto_nodetitle_is_needed($node)) {
    auto_nodetitle_set_title($node);
  }
}
....
function auto_nodetitle_set_title(&$node) {
  $types = node_get_types();
  $pattern = variable_get('ant_pattern_'. $node->type, '');
  if (trim($pattern)) {
    $node->changed = time();
    $node->title = _auto_nodetitle_patternprocessor($pattern, $node);
  }
  else if ($node->nid) {
    $node->title = t('@type @node-id', array('@type' => $types[$node->type]->name, '@node-id' => $node->nid));
  }
  else {
    $node->title = t('@type', array('@type' => $types[$node->type]->name));
  }
  // With that flag we ensure we don't apply the title two times to the same node.
  $node->auto_nodetitle_applied = TRUE;
}
....
function _auto_nodetitle_patternprocessor($output, $node) {
  if (module_exists('token')) {
    $output = token_replace($output, 'node', $node);
  }
  if (variable_get('ant_php_'. $node->type, 0)) {
    $output = auto_nodetitle_eval($output, $node);
  }
  if (variable_get('ant_php_'. $node->type, 0) || module_exists('token')) {
    $output = preg_replace('/[\t\n\r\0\x0B]/', '', strip_tags($output));
  }
  return $output;
}

function token_get_values($type = 'global', $object = NULL, $flush = FALSE, $options = array()) {

  //.......

  // Special-case global tokens, as we always want to be able to process
  // those substitutions.
  if (!isset($tokens['global']['default'])) {
    $tokens['global']['default'] = module_invoke_all('token_values', 'global');
  }

  //.......

}

So when Rules module calls "token_get_values", it retrieves old cached data from Workflow (Pending step, when it is already saved as Approved in database).

Looking for solution right now, will let you know asap.

CommentFileSizeAuthor
#4 php_comparison.jpg116.68 KBx610
rule_settings.jpg177.6 KBx610
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

MGN’s picture

I think this has already been reported in #5 of #262360: Expose control of token caching to functions that call token_replace. You might want to check that and consider if this is a duplicate.

x610’s picture

Status: Needs work » Closed (duplicate)

Thanks a lot! I was looking for this, but found another post.

x610’s picture

Status: Closed (duplicate) » Needs work

MGN, No I wrong. I have this revision, it doesn't help me.
Automatic Nodetitles calls

  else {
    $tmp_tokens = module_invoke_all('token_values', $type, $object, $options);
    $tokens[$type][$id] = $tmp_tokens;
  }

Which caches old workflow step.

x610’s picture

FileSize
116.68 KB

Well, I spend a lot of time of trying to fix this issue. It is much easier for me to make a Custom PHP code condition, where I have $node object with updated Workflow step (see attached image).

Dave Reid’s picture

Status: Needs work » Fixed

This should be fixed with #387648: Stale values for multiple new nodes in a single request (e.g. in FeedAPI). Please re-open if its not the case.

Status: Fixed » Closed (fixed)

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