0

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, \Drupal\Core\Form\FormStateInterface $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, \Drupal\Core\Form\FormStateInterface $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, \Drupal\Core\Form\FormStateInterface $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, \Drupal\Core\Form\FormStateInterface $form_state){
    kint("submit ?"); // never displayed
    die(); // doesn't work, the submit form page loaded successfully
}

function ccustommodule_myform_myvalidate(&$form, \Drupal\Core\Form\FormStateInterface $form_state){
    kint("Validate ?"); // never displayed
    die(); // doesn't work, the submit form page loaded successfully
}
  • Don't prefix the function name with _ – Kevin Sep 7 '16 at 2:25
  • I guess you have to name it right. custommodule_form_submit that should do it. Or you can go and write a constraint plugin for it drupalwatchdog.com/volume-5/issue-2/… – Insasse Sep 7 '16 at 4:45
  • I tested with "custommodule_toto_submit" and cleared caches, but the issue is still there – user2137454 Sep 7 '16 at 6:54
  • Creating my own constraint plugins seems be a solution.. but my way must be work and I would klike understand why a stupid handler adding doesn't work =) – user2137454 Sep 7 '16 at 7:01
  • edit converting to answer... – Berdir Sep 7 '16 at 18:10
1

validate and submit handlers are added to the corresponding form element, e.g. $form['actions']['submit']['#submit'][] = 'foo'. and same for #validate.

Honestly no idea what those methods are used for, never used them. Maybe internally, during form processing.

Also note that the node form currently has multiple submit buttons (save as published, unpublished and so on), so you'd probably need to add your submit to all of those.

0

You could use this foreach function to add the custom submit form to all submit buttons:

  // Set a custom submit function
  foreach (array_keys($form['actions']) as $action) {
    if ($action != 'preview' && isset($form['actions'][$action]['#type']) && $form['actions'][$action]['#type'] === 'submit') {
      $form['actions'][$action]['#submit'][] = 'mymodule_custom_submit';
    }
  }
0

In Drupal 8.2 you can used below code inside file mymodule.module

function mymodule_form_node_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {

   if($form_id == "node_video_form") {  
    foreach (array_keys($form['actions']) as $action) {
        if ($action != 'preview' && isset($form['actions'][$action]['#type']) && $form['actions'][$action]['#type'] === 'submit') {
            $form['actions'][$action]['#submit'][] = 'mymodule_video_form_submit';
        }
    }
  }
}

function mymodule_video_form_submit(&$form, \Drupal\Core\Form\FormStateInterface $form_state) {
  drupal_set_message('It works!');
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.