tags:

views:

53

answers:

3

I have a booking form that's built using options that are retrieved via a third party API and because of the need to have up to date information the results from the API can't be cached (at least not for very long).

The problem I'm having is I've noticed when the form is submitted Drupal is re-calling my _form function which is triggering the API calls again and I'd like to stop it doing that to try and reduce the number of API calls that are made.

Obviously if the validation fails it needs to re-draw the form and the API calls will need to be made again but I'm wondering if there's a way to stop it doing this when the form validates so I can stop it making lots of unnecessary calls to the API.

Thanks for any help.

+2  A: 

Could you store/cache the return values from the API in the $form_state['storage'], so at least if _form gets called every time you can first check storage before making the API calls again.

Simon
+1 - I posted another answer with a bit more details, but this is the right approach to the problem.
Henrik Opel
A: 

First thing, If validation fails, Drupal has a copy of the form, so your form function won't actually be called.

Now to the solution.

  • You can redirect to a new page in your form submit, go avoid the recalling og the form.
  • Instead of calling your form directly in your menu definition make a callback that calls the form. You can then test if the form has been submitted or not and only call your form function when needed.

If you are this worried about the API calls you could also cache it for 5-10 mins which would make my two suggestions obsolete.

googletorp
+1  A: 

You can not avoid the re-creation of the form, if the form is to be processed by Drupal. It is a central part of Drupals form processing workflow: Every form is at least build twice, once for initial output, and at least once again when the post from the client comes in. It can even be more than that, depending on what the form does afterwards, e.g. on redisplay when any validation errors occurs (Usually a cached version gets used by then, but that depends on the specific form).

What you should do is follow Simons suggestion - on the first call to your form builder function, make your API calls and store the results in $form_state['storage']['yourIdentifier'] ('yourIdentifier' being some string not used by standard form processing - usually starting with your modules name). On every call to your form builder function, you check if that results are already in $form_state['storage']. If they are, you just use those, skipping the API calls.

This will avoid the duplicate API calls, and gives you more control on how/when to do them (e.g. you can also clear those results during validation, if a special condition calls for a refetching from the external API).

Henrik Opel
Thanks for the response, I've had a go at getting this working but am having a bit of trouble. I'm setting $form_state in my form function but when I submit the form it's always returning null. Are there any examples anywhere that explain how this works in more detail? I've looked at the ones on the Drupal website but they're not that helpful
pogo
@pogo: It is hard to tell what's wrong without seeing your form builder function, but a common error is using the wrong function signature (http://drupal.org/node/794694#comment-2949122). Maybe you can edit your question to contain the code you use so far?
Henrik Opel