How to change the content moderation state in a custom submit handler? I am trying to change the content moderation state from draft to submitted programatically.  The Content Moderation module adds a field to content types with a dropdown to change the state, but I want to hide this field from users and instead show a custom submit handler that changes the form state.

I can change the default value of the moderation field with code like this:

  $form['moderation_state']['widget'][0]['state']['#default_value'] = "submitted";


But, how do I change the value for the content moderation field using $form_state in the submit handler?

  $form_state-&gt;setValue('moderation_state', 'submitted');`


does not work like it does for fields.

Here is the debug output for kint($form['moderation_state']['widget'][0]):
To change the moderation state you need first to get the entity:

$entity = $form_state-&gt;getFormObject()-&gt;getEntity();


and then change the state:

$entity-&gt;set('moderation_state', 'submitted');


and save:

$entity-&gt;save();


All the code:

/**
 * Submit callback to submit for approval
 */
function draft2submit_callback(array &amp;$form, FormStateInterface &amp;$form_state) {
  // Getting the entity.
  $entity = $form_state-&gt;getFormObject()-&gt;getEntity();
  // Changing the moderation state.
  $entity-&gt;set('moderation_state', 'submitted');
  // Saving the entity.
  $entity-&gt;save();
}