Custom Save and Unpublished button for node While clicking Save as draft button, it will save the node but while it have to redirect to the created node it gives error The website encountered an unexpected error. Please try again later. and $entity-&gt;setPublished(FALSE); is also not working.

function MYMODULE_form_node_form_alter(&amp;$form, FormStateInterface $form_state, $form_id) {
  $form['actions']['submit_unpublish'] = array(
    '#type' =&gt; 'submit',
    '#weight' =&gt; '-10',
    '#value' =&gt; t('Save as draft'),
    '#submit' =&gt; array_merge($form['actions']['submit']['#submit'], 
    ['MYMODULE_draft_submit_callback']),
  );
}

function MYMODULE_draft_submit_callback(array &amp;$form, FormStateInterface &amp;$form_state) {
    $entity = $form_state-&gt;getFormObject()-&gt;getEntity();
    $entity-&gt;set('moderation_state', 'submitted');
    $entity-&gt;setPublished(FALSE);
    $entity-&gt;save();
}
You can't use

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


if you don't have Content Moderation core module enabled

Also make sure you declare the usage of FormStateInterface somewhere in your .module file

use Drupal\Core\Form\FormStateInterface;


Also look in your error logs found in admin/reports/dblog for other possible errors