[development] Retrieving form values during intial form rendering -- SOLVED

Syscrusher syscrusher at 4th.com
Sat Mar 31 03:13:35 UTC 2007


On Friday 30 March 2007 18:19, Mark Fredrickson wrote:
> I think you want to research '#button' and '#after_build'. '#button'
> will send you form values without a full submission. '#after_build'
> will allow you to do something with the built values (node.module uses
> #after_build to prepend the rendered preview version)
> 
> You may have to supply a '#name' element for your button so that it
> doesn't get stuck in 'op'. I'm a little rusty on that point.

And on Friday 30 March 2007 18:28, Peter Wolanin wrote:
> I'll agree with Mark here- I've used this trick a few times.
> 
> Take a look at how node previews are generated.

And now I will write....

File this under, "Boy do I feel stooopid now." You guys are right, though
slightly off on syntax. It's working now.

For the benefit of anyone else who's as dumb as me, here's the solution:

All I needed to do was change '#type' => 'submit' to '#type' => 'button'
in my form array declaration. That's basically it.

I also simplified my life by declaring the $form_values parameter, needed
by my hook_form() function, directly into the menu, as in:

function mymodule_menu($may_cache) {
  global $form_values;
  if ($may_cache) {
    $items = array();
    $items[] = array(
      'path'      => 'mymodule',
      'callback'  => 'mymodule_page',
      'callback arguments' => $form_values,
      'access'    => user_access('access genealogy'),
      'title'     => t('Genealogy'),
    );
...etc....
  return $items;
}

function mymodule_page($form_values=array()) {
  // Generate my page
  $html = drupal_get_form('mymodule_query', $form_values);
}

And finally...

function mymodule_query_form($form_values=array()) {
  $form = array(
    'first_name' => array(
      '#type'     => 'textfield',
      '#title'    => t('First name'),
      '#size'     => 40,
    ),
    'dosearch' => array(
      '#type'     => 'button',
      '#value'    => t('Search'),
    ),
  );
  return $form;
}

I believe Mark is correct about the need for '#name' to be added to the
button, but I haven't tried that part yet. I've done that in another
module, and Mark's recollection matches mine. But the main thing I couldn't
get working, namely the redisplay of the values, works now for all field
types. Right now I have only the one button, so the '#name' business to
tell "which" button isn't an issue for me yet.

Incidentally, looking at node.module for its preview was the first thing I
thought of, but that code's execution flow is pretty complex with all the
foo_invoke_foo() calls, database loads, and such that don't apply to what
I'm doing. I totally overlooked the simple notion of using a non-submit
button to inhibit the call to hook_submit(), but that was the key!

Thanks very much, Gentlemen, for the right answer and the quick reply. I'm
trying to get some Drupal coding done while I'm out of town, and having
this answer tonight will let me make more progress tomorrow during a
no-Internet daytime commitment.

Many thanks!

Scott

-- 
-------------------------------------------------------------------------------
Syscrusher (Scott Courtney)          Drupal page:   http://drupal.org/user/9184
syscrusher at 4th dot com            Home page:     http://4th.com/   


More information about the development mailing list