Combining/reusing forms from different modules

joblogs83 - July 8, 2009 - 06:35

Hi

What is the best way of combining forms in Drupal 6? For example, if I have a page comprising of:

+----------------------
| Field X
| Field Y
+----------------------
| 'user_register'
+----------------------
| Field Z
+----------------------

How would I go about inserting the core user_register module into that page, and how would I control validation/submission?

To date I've managed to use $form['user-register']['#value'] = drupal_get_form('user_register') but I'm having a lot of trouble with a) making some minor alterations to that original user_register form (using hook_form_alter) and b) controlling the validation and submission of the entire page.

I'm struggling to see any decent reading material on how to reuse existing forms - is this not a core principle of Drupal? It would be a ball ache to recode each form from scratch each time you want to use it as part of another form!

Thanks

I'd be interested as well.

traxer - July 23, 2009 - 19:17

I'd be interested as well. For the moment I am experimenting with '#after_build'. I let you know when I find out anything of use. Problems I have identified so far:

  • Both forms bring their own submit buttons.
  • For a given array key, an element might exist in both forms. This is a problem if '#tree' is false, because these elements then have the same name- and id-attribute in the HTML.
  • The value passed to '#element_validate' operates on the whole $form_state array. This means '#tree' cannot easily be changed to true, because the validation function will not find the submitted value.

--
~/.singatrue: file not found

Got it

traxer - August 13, 2009 - 17:14

I found a solution.

The solution is not tested with ajax, neither is it tested with forms that do fancy stuff. FYI: cck does fancy stuff, but I've got a hack.

--
~/.singatrue: file not found

Interesting

joblogs83 - August 25, 2009 - 14:34

I think your needs were a little different to mine based on your solution.

My resolution was something akin to:

* In hook_menu: define a path to load the 'user_register' form.
* In hook_alter: Check the path of the module, and if it's relevant, continue to alter it - otherwise leave it be.

I've now got three seperate registration forms - each serving a different purpose but each sharing a common form parent and overriding it as appropriate. You need to be careful with module weights and ensure your validate + submit handlers work correctly with the core form's functionality. Other than that you should be able to alter the form to your heart's content...

 
 

Drupal is a registered trademark of Dries Buytaert.