I want to check a data when an user submits a form. I set a custom submit handler in form alter, but my custom handler function never gets called. I don’t understand why.
This is my code :
/**
* Implements hook_form_alter().
*/
function custommodule_form_alter(&$form, DrupalCoreFormFormStateInterface $form_state, $form_id)
{
if($form_id == "node_test_form") {
kint("toto"); //toto is displayed when I load the form page
$form_state->setSubmitHandlers(array("_custommodule_test"));
}
}
function _custommodule_test(&$form, DrupalCoreFormFormStateInterface $form_state){
kint("mwahahaa?"); // never displayed
die(); // doesn't work, the submit form page loaded successfully
}
I cleared cache, but the issue is still there.
Edit ##
I tested add a validate handler, the problem persists.. The code :
/**
* Implements hook_form_alter().
*/
function custommodule_form_alter(&$form, DrupalCoreFormFormStateInterface $form_state, $form_id)
{
if($form_id == "node_test_form") {
kint("toto"); //toto is displayed when I load the form page
$form_state->setSubmitHandlers(array("custommodule_myform_mysubmit"));
$form_state->setValidateHandlers(array("custommodule_myform_myvalidate"));
$form_state->getSubmitHandlers(); // display array("custommodule_myform_mysubmit")
$form_state->getValidateHandlers(); // display array("custommodule_myform_myvalidate")
}
}
function custommodule_myform_mysubmit(&$form, DrupalCoreFormFormStateInterface $form_state){
kint("submit ?"); // never displayed
die(); // doesn't work, the submit form page loaded successfully
}
function ccustommodule_myform_myvalidate(&$form, DrupalCoreFormFormStateInterface $form_state){
kint("Validate ?"); // never displayed
die(); // doesn't work, the submit form page loaded successfully
}