I have a custom form, with two different submit actions. Here's my code (edited for brevity):

class EditSlideshow extends FormBase
    public function buildForm(array $form, FormStateInterface $form_state)
        $form['actions']['delete'] = [
            '#type' => 'submit',
            '#value' => t('Delete Slideshow'),
            '#submit' => ['::submitDelete'],
            '#validate' => ['::validateDelete'],
            '#attributes' => ['class' => ['btn', 'btn-blue']],
        $form['actions']['submit'] = [
            '#type' => 'submit',
            '#value' => t('Save Changes'),
            '#submit' => ['::submitForm'],
            '#validate' => ['::validateForm'],
            '#attributes' => ['class' => ['btn', 'btn-blue']],

This mostly works: if I submit the form by clicking on the "Save Changes" button, then validateForm() and submitForm() get called; and if I submit the form by clicking on the "Delete Slideshow" button, then validateDelete() and submitDelete() get called.

But if I submit the form via some other means – for example, by pressing the Enter key while the cursor is inside a text field – then the first action gets called (in this case, the delete action).

That isn't what I want – the submit action should be the default, not delete.

Is there some way that I can tell Drupal to treat submit as the default action instead?

A few things come to mind:

  1. I could register the submit action before the delete action, so that it becomes the first (and therefore the default). I'd rather not do this, though – I want the "Save Changes" button to appear after the "Delete Slideshow" button.

  2. To get around that, I could use CSS to rearrange the buttons. This seems kind of hackish, though.

  3. Instead of implementing "Delete Slideshow" as an action, I could render it as <input type="button"> inside a #markup item, and add some custom JavaScript to detect and handle clicks on this button. However, this means that I'd no longer be able to use FormBase's built-in submit handling to process this "action" – I guess I would need to create my own separate route, controller, etc. (The details are somewhat beyond my understanding of Drupal.)

Any suggestions? (Bonus points if you can show me how to add a confirmation popup to the delete action.) Thanks!

  • 4
    Is this just a matter of setting #weight?
    – Kevin
    Aug 25, 2020 at 13:33
  • 2
    OR move the submit code above delete.
    – No Sssweat
    Aug 25, 2020 at 13:48
  • 1
    Your bonus points comment sounds like a great new question, unless there's already a question with an answer on here.
    – sonfd
    Aug 25, 2020 at 13:53
  • 2
    This is actually determined by the HTML5 spec: A <form> element’s default button is the first Submit Button in tree order whose form owner is that form element.. With that in mind, CSS seems like the right choice for what you need
    – Clive
    Aug 25, 2020 at 18:49


Your Answer

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

Browse other questions tagged or ask your own question.