diff --git a/workbench_moderation.module b/workbench_moderation.module index 8965487..9bc6feb 100644 --- a/workbench_moderation.module +++ b/workbench_moderation.module @@ -2524,3 +2524,53 @@ function workbench_moderation_node_edit_context($arg = NULL, $conf = NULL, $empt // This will perform a node_access check, so we don't have to. return ctools_context_create('node_edit_form', $node); } + +/** + * Helper function checking if the node revision is current one. + * + * It differs from the "workbench_moderation_node_is_current" function on + * the tested revision type: + * - "workbench_moderation_node_is_current" is focused on the "published" + * revision; + * - "workbench_moderation_node_is_current_revision" is focused on the actual + * current revision whatever there is a "published" revision or not. + * + * @param stdClass $node + * The node object to be tested. + * + * @return bool + * TRUE if it is the current revision; Otherwise FALSE. + * + * @see workbench_moderation_node_is_current() + */ +function workbench_moderation_node_is_current_revision($node) { + if (!is_object($node)) { + return FALSE; + } + + // If this node is not moderated, then it is always considered as + // the "current" revision. + if (!workbench_moderation_node_moderated($node)) { + return TRUE; + } + + // If the "is_current" flag is set, then use it. + // It can be missing, when the function is called in a hook_node_presave + // before workbench_moderation is able to set it. + if (isset($node->is_current)) { + return $node->is_current; + } + + // If the node status is FALSE, that means the node does not have currently + // a published revision. + if (empty($node->status)) { + return TRUE; + } + + // If $node->is_draft_revision is not set, we are in the case of the + // node revision publishing. + // Then, the only existing revision is the current one. + // If it is set and is TRUE, we have the draft revision that is also the + // the current revision. + return (!isset($node->is_draft_revision) || $node->is_draft_revision); +} diff --git a/workbench_moderation.rules.inc b/workbench_moderation.rules.inc index 413716a..9891d34 100644 --- a/workbench_moderation.rules.inc +++ b/workbench_moderation.rules.inc @@ -251,7 +251,11 @@ function workbench_moderation_rules_condition_contents_current_state($node, $mod return FALSE; } - $state = (!empty($node->workbench_moderation)) ? $node->workbench_moderation['current']->state : $node->workbench_moderation_state_current; + if (!workbench_moderation_node_is_current_revision($node)) { + return FALSE; + } + + $state = (!empty($node->workbench_moderation_state_new)) ? $node->workbench_moderation_state_new : $node->workbench_moderation_state_current; if ($state != $moderation_state) { return FALSE; @@ -277,7 +281,13 @@ function workbench_moderation_rules_condition_contents_previous_state($node, $mo return FALSE; } - if ($node->workbench_moderation['current']->from_state != $moderation_state) { + if (!workbench_moderation_node_is_current_revision($node)) { + return FALSE; + } + + $state = (empty($node->is_new)) ? $node->workbench_moderation['my_revision']->state : $node->workbench_moderation_state_current; + + if ($state != $moderation_state) { return FALSE; }