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?

up vote 3 down vote accepted

Check that the action you are trying to submit is the correct one. In my case when I had to add a submit handler, I used $form['actions']['submit']['#submit'][] = 'my_custom_submit_handler'; Also, try to add the an extra submit handler and not overwrite the existing one. Finally, try to save your entity in your submit handler and not in the callback. For me also worked the $entity = $form_state->getBuildInfo()['callback_object']->getEntity(); in order to get the entity and alter it in my custom handler.

  • 3
    Yes, this seems to be the problem, though as the submit button is a new element you might need to copy the submit handlers from the original submit button before adding the custom one. – 4k4 Oct 5 '17 at 8:55

As pointed out by @Ismini, the entity has to be saved in the submit handler. The way to do this is, as pointed out by @4k4, to copy the submit handlers for the new button from the original submit handler. The corrected code is shown below.

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_merge($form['actions']['submit']['#submit'], array('MYMODULE_draft2submit_callback')),
  );
}

function MYMODULE_draft2submit_callback(array &$form, FormStateInterface &$form_state) {
  // Getting the entity.
  $entity = $form_state->getFormObject()->getEntity();
  // Changing the moderation state.
  $entity->set('moderation_state', 'submitted');
  // Saving the entity.
  $entity->save();
}

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.