Drupal:以编程方式将Workbench状态从Draft更改为Published

时间:2014-09-11 22:58:17

标签: drupal

我想进行批量操作,并将节点从草稿状态更改为已发布状态。我从之前的更改中创建了一个新版本,但所有修订都默认为草稿。现在我想基本上只发布新版本。 (我正在使用Workbench模块。)

我尝试过像下面这样的事情,但它们似乎都没有效果:

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

$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;

workbench_moderation_moderate($node, 'published');

我尝试使用以下内容而不是node_save进行保存,并认为node_save可能会触发新草稿。

workbench_moderation_node_update($node);

我只是想加载节点,发布草稿,然后再次保存。

知道我做错了吗?

2 个答案:

答案 0 :(得分:5)

workbench_moderation_moderate是正确的,我会这样做批量发布一些节点:

$nodes = node_load_multiple($nodes);
foreach ($nodes as $node) {
    $node->status = 1;
    node_save($node);
    workbench_moderation_moderate($node, 'published');
}

答案 1 :(得分:2)

除了@klidifia所说的,我还想提出另一个对我有用的解决方案。

$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);

我接受klidifia对我的回答的回答的原因是我的解决方案在当前版本上显示消息From Published --> Published on...,而上面的解决方案实际上显示了更合理的工作流程:

From Draft --> Published on...
From Published --> Draft on...