I am trying to write a custom handler for one of the Drupal 7 forms:

function mymodule_formid_alter(&$form, &$form_state, $form_id) {

 array_unshift($form['#submit'], 'mymodule_formid_submit');


function mymodule_formid_submit($form, &$form_state) {
    drupal_add_js('jQuery(document).ready(function () { alert("Hello!"); });', 'inline');

But it is not accessing the handler. Alter method is being called and printing 'mymodule_formid_submit' in the #submit array.

Maybe I am missing something, but exiting answers on Stack Overflow related to custom handlers are not helping in my case.

  • Difficult to tell here but you know that its hook_form_FORM_ID_alter().
    – Alex Gill
    Oct 8, 2012 at 9:33
  • 1
    First comment for ten points: You're not closing your last line (return should be return;).
    – Chapabu
    Oct 8, 2012 at 14:31
  • Why use JavaScript to check it? drupal_set_message('hello'), and you will see 'hello' in a drupal message(not popup) and it works regardless of page redirects.
    – AKS
    Oct 8, 2012 at 16:44
  • I have checked it with plain drupal_set_message and my actual use case is to add a custom js file.
    – nirvana
    Oct 9, 2012 at 6:22

3 Answers 3


I had this same problem trying to edit commerce_product form. For some reason the submit handlers are registered under actual submit button and custom handlers added to the #submit -element weren't registered.

$form['#submit'][] = 'my_custom_handler';
$form['actions']['submit']['#submit'][] = 'my_custom_handler';

So when I added that second line to register my custom handler to button and atleast it works.

  • +1 Thank you @Janne - I found this to work with my generic form submit handler. By generic I mean that on the basis of my success with it, I would therefore think that this solution should work for general Drupal hook form submit handlers. The crucial thing seems to be your advice to add the second line - which makes it work. Feb 9, 2018 at 9:32

In the mymodule_form_alter() function (replace mymodule with your module's name), override the #submit property of the form your want to handle.

function mymodule_form_alter(&$form, &$form_state, $form_id){
    case 'some_form_id':
      // some code to handle the form. 
       $form['#submit'][] = 'mymodule_mysubmit_handler';

Either you have to user hook_form_alter() or hook_form_FORMID_alter() function. So you should write mymodule_form_formid_alter() function.

  • its not working $form['#submit'][] = 'mymodule_mysubmit_handler';
    – nirvana
    Oct 8, 2012 at 9:35
  • have you check that form_alter() is being called ? Oct 8, 2012 at 11:09

Actually, it accessing your handler. BUT, after it, Drupal makes internal redirect to avoid some problems with page refreshing. On the redirected page your handler will not be called (it has been called already), and your JS will not be added to the page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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