4

I'm using Drupal 8.4-rc2. When you enable the Workflow and Content Moderation modules, you automatically get the Editorial Workflow as a demonstration.

The states are as follows:

  • Archived
  • Draft
  • Published

And the transitions are as follows:

  • Create new draft (from draft/published to draft)
  • Publish (from draft/published to published)

How can I programatically call a transition? Or, how can I programatically change the state without calling a transition (although this seems to violate the spirit of having transitions defined in the first place)?

Specifically, I have a node subject to content moderation (states: archived, draft, published). By default, on the edit node page, the node is saved in the original state (so a draft node is saved as a draft). I want to add a second button "save as published" with a submit handler, but I don't understand how I can programatically change the state.

What I do understand so far is that workflows are entities and so are states, so it's different than Drupal 7 in which most workflow-type modules used fields to track what was going on.

I checked the Examples module but there's nothing on configuration management/workflows yet.

9

A great example could be found in Content Moderation module's code.

$entity->set('moderation_state', $new_state);
if ($entity instanceof RevisionLogInterface) {
  $entity->setRevisionLogMessage($form_state->getValue('revision_log'));
  $entity->setRevisionUserId($this->currentUser()->id());
}
$entity->save();
2

I'm not heavily using D8 yet, but in D7's Workbench Moderation module nodes had a moderation_state field that was Draft or Published. Transitions in D7 were typically for User Role workflows whereas in code you typically modified the node states directly via node_load and node_save .... If D8 was inspired by the d7 module, the implementation may be similar.

Did you see the Tests referenced here found in the Examples links: https://www.drupal.org/docs/8/core/modules/workflows/overview and example node moderation field modifications here http://cgit.drupalcode.org/drupal/tree/core/modules/content_moderation/tests/src/Functional/ModerationStateNodeTest.php

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.