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

chx’s picture

Status: Active » Needs review
StatusFileSize
new630 bytes

We 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.

eaton’s picture

Status: Needs review » Reviewed & tested by the community

The 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.

chx’s picture

StatusFileSize
new569 bytes

Dries tells me to use two lines instead of $args = $saved_args = func_get_args(). I hear and oblige.

chx’s picture

Let'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.

dries’s picture

Status: Reviewed & tested by the community » Needs work

Please add code comments. This code change is hard to grok.

chx’s picture

Status: Needs work » Reviewed & tested by the community
StatusFileSize
new1.21 KB
dries’s picture

These 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?

eaton’s picture

StatusFileSize
new2.7 KB

And here's a version with EXTRA JUMBO comments. :-)

dries’s picture

Status: Reviewed & tested by the community » Fixed

Excellent. Committed to CVS HEAD. Thanks a bunch.

mukhsim’s picture

How 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.

chx’s picture

The 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.

sin’s picture

I've tried dunamic form ids with head. Works, thanks! Please review my relevant feature at http://drupal.org/node/91285.

Anonymous’s picture

Status: Fixed » Closed (fixed)