Using a form alter hook, we can add additional submit handlers to a form. The proper way to do this is to load the current submit handlers, add the new one to the array, and reset the validators in the form state:
$submit_handlers = $form_state->getSubmitHandlers(); $submit_handlers[] = 'mymodule_form_submit'; $form_state->setSubmitHandlers($submit_handlers );
First, we will receive all of the currently set submit handlers from the form state as the $submit_handlers variable. We then append a new callback to the end of the array.
Once the $submit_handlers variable has been modified, we will override the form state's submit handler array by executing the setSubmitHandlers method.
You can also use PHP array manipulation functions to add your callback in different execution orders. For example, array_unshift will place your callback at the beginning of the array so that it can run first.