'add another item' in non-CCK Drupal forms
Asked Answered
B

3

5

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?

Buckler answered 2/12, 2009 at 23:12 Comment(0)
A
6

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 ;)

Antibaryon answered 3/12, 2009 at 23:43 Comment(0)
V
2

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.

Vulgate answered 2/12, 2009 at 23:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.