With the site I'm working on, the editor wants to be able to move content through states (draft, needs review, ready to publish, publish). But for the publish state, set up a schedule to publish the content at midnight the next day.

I have my various states configured in workflow. That's working fine.. and workflow schedule is able to switch between these workflow states once the cron runs. However - my problem is with the publish state.

I have a rule that actually publishes the node when the user assigns the workflow state of "publish". But, the rule is also publishing the node even when the workflow state is scheduled for some future time. As soon as the workflow is saved the rule triggers. I'm lost as to how to configure the rule to see the schedule.. ?

Maybe I need two rules: One that publishes as it is now, but on a condition that the workflow isn't scheduled (how to get that condition)? And another one that publishes the node on the schedule.

I'm using Workflow Field.

Can anyone point me in the right direction?

Comments

wheelercreek’s picture

This is my current rule:

{ "rules_publish_article" : {
    "LABEL" : "Publish Article",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "ON" : { "node_presave--story" : { "bundle" : "story" } },
    "IF" : [ { "data_is" : { "data" : [ "node:field-workflow" ], "value" : "5" } } ],
    "DO" : [ { "node_publish" : { "node" : [ "node" ] } } ]
  }
}
wheelercreek’s picture

Okay after spending a couple days watching rules tutorials and trying different things with it, I'm scrapping that whole idea. I just can't seem to get what I need out of rules. So I decided, this shouldn't be that hard, I'll just write a custom module to publish the nodes on cron job if that workflow state is scheduled. I'm really close but now hitting a new snag. Here's my code:

/* 
* Implement hook_node_presave(); 

* publish node if workflow state is set to publish immediately (not scheduled)
* unpublish node if WF is marked to any state besides published
*/ 
function ssnwf_node_presave($node) {
    $field_workflow = 0; 
    if(isset($node->field_workflow[$node->language][0]['workflow']['workflow_sid'])) { 
        $field_workflow = $node->field_workflow[$node->language][0]['workflow']['workflow_sid'];
    }
    if($field_workflow == SSN_WORKFLOW_PUBLISHED) { //published
        //if there is a scheduled transition it will be handled by the cron job function below. 
        //if there's no scheduled time, go ahead and publish now.
        if(empty($node->field_workflow[$node->language][0]['workflow']['workflow_scheduled'])) { 
            $node->status = 1; 
        } 
    }
    
    // unpublish node if WF is marked to any state besides published
    if($field_workflow < SSN_WORKFLOW_PUBLISHED) {
        $node->status = 0;
    }
}

/* 
* Implement hook_cron()
* publish node on schedule (cron run) if workflow is updated to publish & scheduled
*/
function ssnwf_cron() {
    $time = REQUEST_TIME; 
    $result = db_query('SELECT wft.nid, n.vid FROM workflow_scheduled_transition wft, node n
        WHERE wft.scheduled < :time
        AND wft.sid = :sid
        AND wft.nid = n.nid', array(':time' => $time, ':sid' => SSN_WORKFLOW_PUBLISHED)); 
    
    $update_these = array();
    $num_updated = 0;
    foreach($result as $item) { 
       db_update('node')
            ->fields(array(
                'status' => 1,
            ))
            ->condition('nid',$item->nid)
            ->execute();
        
        $num_updated = db_update('node_revision')
            ->fields(array(
                'status' => 1,
            ))
            ->condition('nid',$item->nid)
            ->condition('vid',$item->vid)
            ->execute();
        
        $update_these[] = $item->nid;
    } 
    cache_clear_all();
    watchdog('ssnwf', 'Number of nodes updated = '.$num_updated);
    $updated = implode(',', $update_these); 
    watchdog('ssnwf', 'These scheduled nodes were published = '.$updated);
}

When the cron runs, I'm able to verify that the nodes actually do get published - with those Watchdog messages. However, Workflow is for some reason resetting them back to be unpublished?? The workflow cron function runs after mine here, and takes care of the scheduled workflow state change.. but why are the nodes reset back to unpublished?

I don't get why this is so difficult.

wheelercreek’s picture

Title: Need rules help to publish node with workflow schedule » How to use workflow schedule to publish node?
wheelercreek’s picture

Got this worked out - in case anyone else runs into this issue. My code above is pretty close, I just added more to actually perform the scheduled Workflow field state transitions myself (updating the field tables), and then clear out records from the workflow_scheduled_transition table so that when the actual WF cron function runs it doesn't find them.

I still don't know why the nodes were getting set back to unpublished, but clearing out the scheduled_transitions table seems to fix it.