8

I would like to do a bulk operation and change nodes from a state of Draft to Published. I created a new revision from a previous change, but all revisions default to Draft. Now I would like to basically just publish the new revision. (I am using the Workbench module.)

I have tried doing things like the below, but none of them seem to work:

$node->workbench_moderation['current']->published = "1";

or

$node->workbench_moderation['current']->from_state = "draft";
$node->workbench_moderation['current']->state = "published";
$node->workbench_moderation['current']->published = "1";

$node->workbench_moderation['published']->from_state = "draft";
$node->workbench_moderation['published']->state = "published";
$node->workbench_moderation['published']->published = "1";

$node->workbench_moderation['my_revision']->from_state = "draft";
$node->workbench_moderation['my_revision']->state = "published";
$node->workbench_moderation['my_revision']->published = "1";
$node->workbench_moderation['my_revision']->current = TRUE;

or

workbench_moderation_moderate($node, 'published');

I've tried saving using the below rather than node_save as well, thinking maybe the node_save triggered a new draft.

workbench_moderation_node_update($node);

I just want to simply load the node, publish the draft, then save it again.

Any idea what I am doing wrong?

10

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...
0

@Keven

The second solution is the correct one! You just have to load the most recent revision with node_load. node_save() triggers the workbench_moderation_moderate() function so you don't have to do it after node_save() manually!

$query = db_select('workbench_moderation_node_history', 'wmnh');
$query->addField('wmnh', 'vid');
$query->condition('wmnh.nid', $nid);
$query->condition('wmnh.current', 1);
$current = $query->execute()->fetchField();

// or you can get the latest revision id by loading the node without revision id:
$node = node_load($nid);
// Altough you can get node revision id from node object itself i prefer using the workbench_moderation property.
// $current = $node->vid;
$current = $node->workbench_moderation['current']->vid;

$node = node_load($nid, $current);
$node->workbench_moderation_state_new = workbench_moderation_state_published();
$node->revision = 1;
node_save($node);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.