Suppose i need to record all form submissions in a site, i mean all of them. I would add a new submit handler to be executed after each forms submit handler, like $form['#submit'][] = 'log_submit' , right?

The problem is that some forms may not have the submit handler inside $form['#submit'] array but in $form['buttons'] or $form['#actions'] etc...

How would i add my own submit handler to ALL of these forms, in one hook_form_alter() and in a clean way?


1 Answer 1


You can use hook_form_alter(&$form, &$form_state, $form_id) without traditional if inside. Then it'll work on all forms.

Problems start when submit handlers are attached not to form, but to buttons. If you have to take care about that, you will need to scan recursively form tree, find all elements with ['#submit'] set and attach your handler to them. to do this, write recursive* function. Drupal mixes properties with children elements, so to avoid scanning properties, use element_children(&$elements, $sort = FALSE).

*Of course you can make it iterative, too, should be faster, but recursion is more intuitive approach.

  • @PanChrono and would you mind telling us why? Maybe there is cleaner solution for what's your desired end result.
    – Mołot
    Jul 8, 2013 at 13:01
  • As stated above i want to log each forms submission, previous values - submitted values, another question would be how to test that...
    – Pan Chrono
    Jul 8, 2013 at 13:19
  • @PanChrono I meant why do you need to log all that. If, for example, for HTTP testing purposes, it might be better to let Ettercap sniff your (or server's) LAN. Many forms, like Search or Views exposed filters hardly makes sense to log on their own, but will generate a lot of noise.
    – Mołot
    Jul 8, 2013 at 13:23

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.