I have a form which is called from a page function. The page initially makes some external calls to get #default_value data which gets passed to drupal_get_form(). When the form is submitted, the page function gets called before the submission handler. Why is that? I don't get it and I don't want that to happen because it then connects to an external data API for no reason.

Does anyone know of a good place to find the calls order for a default submission handler? I'm unable to debug this.

There is an AJAX update when the form inputs refresh, but I don't see how that matters.

  • Have you tried var_dump($form['#submit']) from hook_form_alter? There may be some information in there. You can array_unshift($form['submit'],'my_custom_callback_function') to place your callback first in the callstack. Is that at all what you're looking for? – Morgan Delaney Apr 5 '12 at 18:30

To understand this, we should step back a bit in look at how forms are used regardless of what CMS (if any) is in use.

All <form> tags have an action attribute which define what page the browser should send the data to. If action is left blank (or missing) the browser will assume it should send the data to the same URL it is currently on. In this scenario the URL will be loaded to get the form and then again to send the data.

This is how the Form API works (by default). When you define a URL (using hook_menu I presume) that presents a form to the user, the browser will use the same URL to load and send the form.

The menu router in Drupal has to be run first so it can determine what page to display. Only after that does Drupal see that it has a form and then run the submit callback. In other words, you cannot change the order of these functions.

You can provide an #action attribute in your Form API implementation which (combined with a new hook_menu entry) should let you separate your form load/submit functions.

  • 1
    Note that providing a custom #action callback means that you loose all the built-in (security) features of the Form API, e.g. CRSF protection. You really shouldn't do this. Instead, you could for example cache the data from the external service using the cache API(). – Berdir Nov 22 '11 at 7:02
  • you guys are awesome thanks! it seems like a silly question now. Cache api looks simple too. – Will Nov 22 '11 at 8:56

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.