'add another item' in non-CCK Drupal forms

亡梦爱人 提交于 2019-12-21 04:58:08

问题


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:


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




回答2:


http://plosquare.blogspot.com/2010/02/drupal-6-adding-form-fields-dynamically.html




回答3:


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.



来源:https://stackoverflow.com/questions/1836549/add-another-item-in-non-cck-drupal-forms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!