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->setPublished(FALSE); is also not working.

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

function MYMODULE_draft_submit_callback(array &$form, FormStateInterface &$form_state) {
    $entity = $form_state->getFormObject()->getEntity();
    $entity->set('moderation_state', 'submitted');

You can't use

 $entity->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

| improve this answer | |
  • after enabled Content Moderation core module then also its same, after that I commented $entity->set('moderation_state', 'submitted'); then its working :) thankyou. – Mukunda Bhatta Mar 26 '19 at 9:25
  • I have tried it again and after enabling Content Moderation module workflows is also enabled and setting moderation_state works. – GiorgosK Mar 26 '19 at 10:39

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.