[development] How to pass form values to page that actually does something.

David Metzler metzlerd at metzlerd.com
Tue May 11 03:46:26 UTC 2010


I generally use a static variable object caching mechanism for this:

function mymodule_class_factory() {
  static $object = ''; 
  $path = drupal_get_path('module','mymodule'); 
  require_once($path.'mymodule_class.inc'); 
  if (!$object) {
    $object = new mymodule_class();  
  }
  return $object; 
} 

Now whenever I need my complex data storage mechanism, I simply call the factory method, which will (the first time it is called) initialize the object.   Thereafter it returns the instantiated object... like this: 
 
$o = mymodule_class_factory(); 

The submit function can get the object and invoke methods in the class. This means that business logic is all in the class definition, and you can use it equally well in the the rendering function,
the form submit handler, validation handler and rendering function.  You can also put the external database connectivity logic into the contructor for the object, and know that it's only used when needed.   Saves on module memory footprint too.   If you want to you can move the require_once into the menu hook file include mechanism if you find that more elegant. 

Dave


On May 10, 2010, at 9:37 AM, Bayne, Sam wrote:

> I'm quite frustrated with the Form API right now, so please forgive my tone.
> 
> I've got a form that has a bunch of inputs, all of them multiselects, that allow the user to choose what classes to see in our schedule.
> (our class schedule is in an external database).  I've got a bunch of code in the form_submit function that builds a very elaborate sql query to get the list of requested classes.
> 
> Unfortunately, the form_submit function is not actually permitted to display anything except through drupal_set_message().
> 
> So I have to either pass my complicated sql query, or all the contents of the form fields to some other page through $form_state['redirect'].
> 
> How do I do that without shoving it all into the querystring?
> 
> Right now, I'm looking at the performance issues of:
> 
> A: storing the query as a string in the database with a query_id, then sending a query_id in the redirect,
> B: storing the result of the query in the database with a result_id, then sending a result_id in the redirect.
> 
> Is there some other way to accomplish what I want?
> 
> p.s. I've looked at the search module, it basically shoves the form input into the querystring of the redirect. That'll work for a single input, but multiple array inputs will be a huge pain.
> 
> 



More information about the development mailing list