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(&$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(&$form, FormStateInterface $form_state) {
\Drupal::logger('search_redirect')->notice("the function is triggered"); // Not happening
\Drupal::service('messenger')->addStatus("the function is triggered"); // Not happening
}
function _second_submit(&$form, FormStateInterface $form_state) {
\Drupal::logger('search_redirect')->notice("second_submit"); // Not happening
\Drupal::service('messenger')->addStatus("second_submit"); // Not happening
}