How do I alter a form submission handler? I want to alter the form submission handler submit for the search block with a custom module.
Setting  $form['#attributes']['class'] works, but the submission handler doesn't seem to be triggered with any of the methods that I have alternatively found in many core and third-party modules.
/**
 * @file
 * Redirect search from search block to XXX.
 */

use Drupal\Core\Form\FormStateInterface;
 
/**
* Implements hook_form_FORM_ID_alter().
*/
function search_redirect_form_search_block_form_alter(&amp;$form, FormStateInterface $form_state) {
    $form['#attributes']['class'][] = 'test-class'; //Working
    $form['#submit'][] = 'search_redirect_search_block_form_submit';
    $form['actions']['submit']['#submit'][] = '_second_submit';
}

function search_redirect_search_block_form_submit(&amp;$form, FormStateInterface $form_state) {
  \Drupal::logger('search_redirect')-&gt;notice(&quot;the function is triggered&quot;); // Not happening
  \Drupal::service('messenger')-&gt;addStatus(&quot;the function is triggered&quot;); // Not happening
}

function _second_submit(&amp;$form, FormStateInterface $form_state) {
  \Drupal::logger('search_redirect')-&gt;notice(&quot;second_submit&quot;); // Not happening
  \Drupal::service('messenger')-&gt;addStatus(&quot;second_submit&quot;); // Not happening
}
Stupid question but have you cleared cache to register the new hook?
Also, try inspecting the form in hook_form_alter to make sure you are attaching the handler to the correct action.
Lastly, I've found that I usually need to reorder the array to include my handler first in order for my actions to actually trigger on form submissions using Drupal core forms that have existing handlers.
As an example, I did like:
$form['actions']['submit']['#submit'] = array_reverse($form['actions']['submit']['#submit']);

which will reverse the array. I also sometimes use array_unshift to pop my handler at the start of the array if the other handlers need to retain their order.