views:

721

answers:

3

CCK for Drupal has the handy feature of allowing a single CCK field to accept multiple values via a "add another item" button that results in an array of data in the node.

I'm wondering if there's any easy way of adapting this for a non-CCK form that I'd be calling by drupal_get_form. I'd rather not have to re-invent the wheel with multi-step form techniques.

Anyone done this?

+1  A: 

Just a stub of an answer, sorry. If nobody else will give you a better answer and I have time, tomorrow I will elaborate a bit more.

I believe that functionality is done by attaching a drupal behavior (from the Drupal standard js library) to the field you are considering. If I am wrong, then it is simply an "ad-hoc" jQuery function.

However that's the easy part! ;) The tricky one, is that the functionality is achieved through AJAX and not by simply generating an extra form field client-side. This is so because the form API must regenerate the form entirely, otherwise - on submission - additional fields that where not originally sent to the browser will be simply ignored (this is intended, in order to prevent injection).

HTH at least a bit! Sorry for being so concise.

mac
+3  A: 

It would be nice if they had included a more reduced example, but this Introduction to AHAH in Drupal, along with this post on adding dynamic form elements using AHAH should cover pretty much all you need (which, unfortunately, is quite a bit).

The way CCK does this (mainly in content_add_more_js() in content.node_form.inc) is a bit more convoluted, as they need to cater for the generalization for different field types, as well as adding some extra stuff to allow for smooth tablesort Drag&Drop functionality. So unless you need the latter, I'd rather not use it as an example, as it gets a bit confusing ;)

Henrik Opel

related questions