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.

This entry was posted in Web Development and tagged , . Bookmark the permalink.

Leave a Reply

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