Drupal - How do you implement a custom submit handler in hook_form_alter()?

According to the example in the documentation it's the same as for Drupal 7:

$form['actions']['submit']['#submit'][] = 'mymodule_upload_enabled_types_submit';

You can find similar (working) logic in

  • contact_form_user_form_alter()
  • editor_form_filter_format_form_alter()
  • locale_form_language_admin_edit_form_alter()

And a few others besides.


You can add how many submit handler you want using the following code:

 $form['actions']['submit']['#submit'][] = 'mymodule_what_ever_function';

If you want to add a submit handler after the default submit handler, (a submit handler that will be called after the submit callback), you can use the following.

$form['#submit'][1] = test_function;

To remove the submit handler:

unset($form["actions"]['submit']);

The behavior is either changed or differs from case to case. With the Node Delete Multiple confirmation form

$form['actions']['submit']['#submit'][] = 'mymodule_upload_enabled_types_submit';

@Clive's code causes Drupal to ignore the main handler. In this case add the handler to the $form['#submit'] section:

$form['#submit'][] = 'mymodule_upload_enabled_types_submit';

Tags:

Forms

8