Dec 01, 2020 in Drupal 8

Moderation states help you give entities a state in workflows. Here is how to update your entity programmatically to a published workflow.

use \Drupal\Core\Entity\RevisionLogInterface;

$new_state = 'published';
$entity->set('moderation_state', $new_state);
if ($entity instanceof RevisionLogInterface) {
  $entity->setRevisionLogMessage('Changed moderation state to Published.');
  $entity->setRevisionUserId($this->currentUser()->id());
}
$entity->save();