以编程方式将工作台状态从草稿更改为已发布


8

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

我已经尝试过执行以下操作,但是似乎都没有用:

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

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

知道我在做什么错吗?

Answers:


11

我发现可以使用两种解决方案:

首先:

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

注意:我特意放了workbench_moderation_moderate()之后,node_save()因为在我的情况下node_save()会触发新的草稿。创建草稿后,我将发布该草稿。

第二:

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

由于状态消息,我将采用第一种解决方案而不是第二种解决方案。第一个显示当前版本下的两个消息:

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

而第二种解决方案仅显示一条消息,实际上并没有多大意义:

From Published --> Published on...

0

@凯文

第二种解决方案是正确的!您只需要使用node_load加载最新的修订版。node_save()会触发workbench_moderation_moderate()函数,因此您不必在node_save()之后手动进行操作!

$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);
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.