3

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?

3 Answers 3

4

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.

1
  • 4
    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.
    – 4uk4
    Commented Oct 5, 2017 at 8:55
7

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();
}
1
  • 1
    Assuming the form is saving an entity's content, running $entity->save() in the submit handler is going to trigger the save function twice. Is there any way to avoid this? I assumed that by setting the values on on $form_state (in reference with the &), it wouldn't require entity->save()?
    – jozi
    Commented May 13, 2022 at 22:50
-1
   function MODULE_form_alter(&$form, 
      \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
      if($form_id == 'node_contentype_form') {
        array_unshift($form['actions']['submit']['#submit'],
                                         'MODULE_submit_handler');
       }
   }

   function MODULE_submit_handler(array &$form, 
          \Drupal\Core\Form\FormStateInterface $form_state) {
       $form_state->setValue('title', [['value' => 'new title']]);
   }
1
  • While the code might demonstrate what's wrong, it doesn't explain why. Please add some description that answers what's wrong with the OP's code.
    – Shawn Conn
    Commented Oct 18, 2018 at 0:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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