[development] FAPI and _display hook

David Metzler metzlerd at metzlerd.com
Thu Feb 7 14:38:20 UTC 2008


FYI: Another technique I've used is to call my form function myself.   
It adds an extra call but here's an example:

function non_form_func()
{
   $output = drupal_get_form('myform');
   $output .= 'some html outside of the form.'
   $form = myform();

   // If you want you can call the drupal form alter hook functions  
here to
  // make sure you get modifications from other modules, but I don't  
have that code here

   if ($form['somefield']['#default_value'] == 'someother value')
   {
       // do some stuff specific to forms here
   }

   return $output;
}
On Feb 7, 2008, at 3:17 AM, Ivan Sergio Borgonovo wrote:

> On Thu, 7 Feb 2008 10:30:12 +0100
> "emanuele quinto" <emanuele.quinto at gmail.com> wrote:
>
>> On the _validate you can do whatever you want: save values with
>> set_variable or global variable (I know, I know...) or in  a table.
>> In principle you don't have to wait the _submit.
>
> Yep... but somehow you're just making a copy (a global one) of
> something that should be already there and accessible as much as
> $_POST is.
> FAPI does a lot of valuable work that is hard to take out unless you
> use some not really clean technique that mix form processing,
> rendering and stuff related to value passing.
>
>>> At the end of drupal_get_form $form_values/status should be fully
>>> populated[1] so you could access it through such a global...
>>
>> $form_status['values'] is populated (in D6):
>> * if you set $form_status['rebuild'] (in _validate)
>> * if you set some value in $form_status['storage']
>>
>> They are available in a _form_alter hook where you can change the
>> apparence of the form.
>
> Somehow... in this use case... form is gone, it already finished its
> task, it checked for spoofing, filled and validated the submitted
> data and they are in $form_status.
>
> All these techniques look like getting in the FAPI from the back door
> to steal what you need and make it available to other parts of your
> code.
>
> eg. I could just write a function that get called from whatever hook
> FAPI is going to call that call another function with a static
> member to take a snapshot of $form_status and then I can call the same
> helper function to get back a copy of the $form_status... but still,
> that's like stealing, not asking.
> Of course this doesn't solve the problem to know when $form_status is
> ready... that's why we have hooks... etc... etc...
>
> Every module writer could do the same and we will end up with 20
> copies of $form_status and 20 similar thief functions.
>
> I don't think that using validated data without pushing them to the
> DB or session in more than one place is something unusual.
>
> Maybe part of drupal_get_form could become a sort of hook that
> process $_POST before anything else get called, you could book $_POST
> processing in menu hook and drupal_get_form should just render the
> form.... but well this looks pretty radical and maybe completely
> wrong since I don't know enough about FAPI internals.
>
>
> -- 
> Ivan Sergio Borgonovo
> http://www.webthatworks.it
>



More information about the development mailing list