Drupal - Change Workbench state from Draft to Published programatically
There are two solutions which I have found will work:
The First:
$nid = 1234;
$node = node_load($nid);
$node->body['und'][0]['value'] = 'new body';
$node->revision = 1;
$node->log = 'State Changed to published';
node_save($node);
workbench_moderation_moderate($node, 'published');
NOTE: I intentionally put workbench_moderation_moderate()
after node_save()
because in my case node_save()
will trigger a new draft. After the draft is created, I publish that draft.
The second:
$nid = 1234;
$node = node_load($nid);
$node->body['und'][0]['value'] = 'new body';
$node->workbench_moderation_state_new = workbench_moderation_state_published();
$node->revision = 1;
$node->log = 'State Changed to published';
node_save($node);
I am going with the first solution over the second solution because of the status messages. The first shows the two messages under the current revision:
From Draft --> Published on...
From Published --> Draft on...
whereas the second solution only shows one message which doesn't really make much sense:
From Published --> Published on...