If I have the workbench moderation module enabled and I would like to unpublish my content, how would I accomplish this?

I tried this but it didn't work:

$node = Node::load(1827);
$node->setPublished(FALSE);
$node->save();

Comments

albertski created an issue. See original summary.

haynescw’s picture

$node = Node::load(1827);
$node->setPublished(FALSE);
$node->set('moderation_state', "draft");
$node->save();

But... I have just found a bug where published content that is returned to draft status is still visible to anonymous users. Working on trying to find a known issue, if not creating one.

haynescw’s picture

albertski’s picture

Status: Active » Closed (outdated)

Thanks this worked like a charm!

albertski’s picture

Status: Closed (outdated) » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

mohsinkhanmca’s picture

How to change user status?

CFG OCC’s picture

Here's an example of how content moderation was implemented on a migration POST_ROW_SAVE event on Drupal 8: -

    public function onPostRowSave($row)
    {
      //Get this node ID from migration
      $rowIds = $row->getDestinationIdValues();
      $rowId = $rowIds[0];
      
      //Set the node to published including content moderation
      $node = Node::load($rowId);
      $node->setPublished(TRUE);
      $node->set('moderation_state' , 'published'); //Set content moderation flag to published
      $node->save();
    }
sourabhutani’s picture

Hi

Not working for me as i tried below code. I'm using workbench moderation 8.x-1.2 version.
$node = Node::load(1827);
$node->setPublished(FALSE);
$node->set('moderation_state', "draft");
$node->save();

imperator_99’s picture

@haynescw try setting the moderation state to 'archived', which will unpublish the revision (assuming you have an 'archived' state). The draft moderation state doesn't actually unpublish latest revision, ie:

$node = Node::load(1827);
$node->setPublished(FALSE);
$node->set('moderation_state', "archived");
$node->save();

Or, to do more than one:

$nids = \Drupal::entityQuery('node')->execute();
$nodes =  \Drupal\node\Entity\Node::loadMultiple($nids);
foreach ($nodes as $node) {
  $node->setPublished(FALSE);
  $node->set('moderation_state', "archived");
  $node->save();
}

Drupal::entityQuery() can have conditions added to it to restrict the content types or anything else.

Cheers,
Jesse.

avinash_thombre’s picture

If you want to unpublish the nodes in array of content types programmatically, use following code in any of your .intsall file inside hook_update:

function pfe_brand_update_7002(){

$cont_types = array('article', 'page');
foreach ($cont_types as $type) {
$cont_nodes = node_load_multiple(array(), array('type' => $type));
foreach ($cont_nodes as $cont_node) {
$cont_node->status = 0;
node_save($cont_node);
}

}
}

srutheesh’s picture

Drupal 9:

Use following code
Unpublish :

	$nodesg->setUnpublished();
	$nodesg->save();

Publish :

	$nodesg->setPublished(TRUE);
	$nodesg->save();