There are many questions about a custom form submit handler not being called, but in my case, the handler is being called; however, the entity is not being saved. Specifically, I have a custom submit handler on a content type; when I press the button to call the handler, nothing is saved in the database:
function MYMODULE_form_node_CONTENTTYPE_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$form['actions']['submit_publish'] = array(
'#type' => 'submit',
'#weight' => 999,
'#button-type' => 'danger',
'#value' => t('Submit for approval'),
'#submit' => array('MYMODULE_draft2submit_callback'),
);
}
function MYMODULE_draft2submit_callback(array &$form, FormStateInterface &$form_state) {
$form_state->setValue('moderation_state', 'submitted');
}
So I thought since the node is not being saved, maybe I have to do it manually. Following this article, I did this:
function MYMODULE_draft2submit_callback(array &$form, FormStateInterface &$form_state) {
$form_state->setValue('moderation_state', 'submitted');
$node = $form_state->getFormObject()->getEntity();
$node->save();
}
However, in this case, the node is saved, but none of the submitted values (the node body, for example) are saved; everything added by the user is ignored, as is the setValue()
in my callback.
I only want to set the moderation_state
in my custom form submit handler; in all other ways, I want my custom submit handler to be the same as the normal submit handler (i.e., I want it to save the node). What am I doing wrong?