Changing the Default Content Moderation State in Drupal 8

I have been working with the Content Moderation module in Drupal 8 and one slight niggle we came across was the default option for the Moderation state.

If the current state was In Review we would like the Change to option to also be In Review rather than Draft.

Current default moderation state

The desired default state option

This means the default is to keep the entity in it’s current state rather than switching to a new state. Switching to a new state is then an active task for the editor.

I did this by implementing hook_form_alter() in a custom module.

 * Implements hook_form_alter().
function my_module_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {

if ( array_key_exists('moderation_state', $form) ) {

$node = $form_state->getFormObject()->getEntity();

$moderationState = $node->get('moderation_state')->getValue('state');

if (isset($moderationState[0]['value'])) {
 $form['moderation_state']['widget'][0]['state']['#default_value'] = $moderationState[0]['value'];



In this function we first check if the form contains the moderation_state key.

Next we load the node so we can access it’s properties.

The moderation state is then retrieved from the node.

The last part is to check if the moderation state has a value. If we are creating a node this will not be set.

In the if statement we then set the default value to the current state.

10 thoughts on “Changing the Default Content Moderation State in Drupal 8”

  1. Thank you very much,

    This fixed my issue. I’m wondering if it’s supposed to behave like that by default or isn’t it just implemented yet.

  2. Glad it’s helped.

    I think it’s probably one of those small bugs that hasn’t been looked at yet.

  3. Hi ian,

    I came over your post while I was looking to print the last moderation state in node.twig , to show a user, there is an unpublished state of his node. You use the hook_form-alter function. How can I get a twig variable of the last unpublished state with the preprocess_node function in my theme-file?

    Sorry I’m a site builder, no coder πŸ˜‰

  4. In hook_preprocess_node() you could use the following to get the current state


    Does that help?

  5. Thank you, but this code prints the moderation state of the published revision. I need the moderation state of the last revision (if there is an unpublished revision).

    For example, if there is a new revision of a node in draft state (maybe the user has edited the node), I want to show the user an info on the node “there is a new state waiting for review”.

  6. This will check if you are currently viewing the default revision and if there are any new revisions.

    // Check if we are viewing the default revision.
    if ($vars['node']->isDefaultRevision() == TRUE) {

    // Get all of the revision ids.
    $revision_ids = \Drupal::entityManager()->getStorage('node')->revisionIds($vars['node']);

    // Check if the last item in the revisions is the loaded one.
    $last_revision_id = end($revision_ids);

    if ($vars['node']->loadedRevisionId != $last_revision_id) {
    // Load the revision.
    $last_revision = \Drupal::entityManager()->getStorage('node')->loadRevision($last_revision_id);
    // Get the revisions moderation state.
    $last_revision_state = $last_revision->get('moderation_state')->getString();
    $vars['new_revision_message'] = 'There is a new ' . $last_revision_state;


  7. Thank you ian, your code helped me a lot. One thing I’ve found was, the last revision check doesn’t worked because “$vars[‘node’]->loadedRevisionId” gives no ID. I ‘ve changed this to “$vars[‘node’]->getRevisionId()”.

    Maybe this could be done better, but it was the only solution I’ve found. Another thing I found was the info, that entityManager() is deprecated in Drupal 8. Instead, you could use entityTypeManager() – this worked for me.

    Thank you again, no one else could help me with this πŸ™‚

  8. Glad it helped you out.

    I didn’t pick up on entityManager() being deprecated so thanks for letting me know, we’ve helped each other out.

Leave a Reply

Your email address will not be published. Required fields are marked *