In Drupal 5.0, the new FormAPI architecture uses hook_forms() to map multiple form ids (like story_node_form and page_node_form) to a single builder function (like node_form). This works fine when a static set of forms is being mapped, but in some cases it's necessary to create n forms, each with a different sequential ID, that all map to the same set of form builder and processing functions.
karma.module is one such example -- it generates a form for every piece of content in a list, allowing each to be rated. If the same form ID is used, the processing functions will fire *as many times as there are pieces of content* on the page. Clearly, that's no good.
Comments
Comment #1
chx commentedWe just pass the form arguments to hook_forms and make sure it's hook_forms every time there is a cache miss in drupal_retrieve_form. This change adds to the API but and does not require changes to any existing modules.
Comment #2
eaton commentedThe very simple patch that chx posts would, indeed, allow modules that *REALLY* need to do dynamic form id mapping to handle such things in their hook_forms() implementation. Very clean.
Comment #3
chx commentedDries tells me to use two lines instead of
$args = $saved_args = func_get_args(). I hear and oblige.Comment #4
chx commentedLet's note that this change a) does not make the cache less efficient for current modules because they do not have dynamic form ids b) there is nothing to update in hook_forms implementations because I merely added a parameter and PHP is happy with functions accepting less parameters than given to them.
In other words, this is a harmless patch which reopens some avenues which we closed unintentionally.
Comment #5
dries commentedPlease add code comments. This code change is hard to grok.
Comment #6
chx commentedComment #7
dries commentedThese code comments need work. Aside from some typos, they are very abstract: things like "dynamic hook_forms implementations can do their work" have very little value. What is it what they have to do at this particular time and why?
Comment #8
eaton commentedAnd here's a version with EXTRA JUMBO comments. :-)
Comment #9
dries commentedExcellent. Committed to CVS HEAD. Thanks a bunch.
Comment #10
mukhsim commentedHow does form_submit work in this case?
How is it called from those dynamic forms and how does submit function know which form is calling it?
Delf.
Comment #11
chx commentedThe first parameter you get is the $form_id from which you could regexp out whatever. Or you can use $form['#whatever'] to pass around data, as node_form saves $node into $form['#node'] . FAPI is happy to not deal with any attributes it does not use.
Comment #12
sin commentedI've tried dunamic form ids with head. Works, thanks! Please review my relevant feature at http://drupal.org/node/91285.
Comment #13
(not verified) commented