I couldn't find an issue for this in CCK, but about once a week someone asks about dynamically adding more fields when using the link module. Most CCK field types when displayed as multiple give the user x number of fields. If they've filled them up then they must hit 'Preview' to get some more. This isn't clear for a variety of reasons.

With jQuery in core, let's work to dynamically add fields via javascript. Although I think this would have to be handled by individual field modules (as the creation of more fields on Preview is), there's no reason why every module should have to reinvent the wheel when it comes to the javascript necessary.

I've put up an example module which includes this functionality. Although I wrote the javascript with core in mind, I realized that there's no place in core which uses this ability (other than upload module, on which this code is based). The javascript file is called is called 'link.js' in the example, but included as part of CCK, I think 'more.js' would be a suitable name.

Demonstration: http://quicksketch.org/drupal5/node/add/links

CommentFileSizeAuthor
more.js_.txt2.07 KBquicksketch

Comments

yched’s picture

I did not really look at your code yet, but this looks very interesting.

Ideally, the "sort-of-mythical-yet-to-be-kicked-off" next version of cck should let field modules define a hook for a _single value_ widget, letting content.module consistently handle multiple values and the number of widgets to display on node form.
This could then allow interesting features like 'define the exact number of multiple values', or this 'ajax-add more widgets', which currently have to be supported field-module per field_module.

BTW, your demo link seems dead ?

dodorama’s picture

Woah! this would be a great addition.

quicksketch’s picture

Hmm demo link should work. Looks like my DB server went down sometime this morning. Probably just an unfortunate coincidence.

Consistency in adding multiple fields would be great. It'd also make field development much easier. This javascript file sort of fits into that mentality. In most fields, there is already a function which renders exactly one widget. Then if multiple is defined is just calls it 2-3 times. The AJAX request made in this method goes to a menu callback which also calls that function, but then just JSON encodes it and returns it back to jQuery. jQuery prepends the new field to the current list.

As CCK moves towards handling multiple fields centrally, this method would still work. The JS degrades nicely so if jQuery isn't available, the previous behavior (reload the page and add two fields) still works.

moshe weitzman’s picture

we have been discussing these AHAH form fields over at http://groups.drupal.org/node/3171. there is working code there that is well integrated into fapi.

Laurentvw’s picture

I have tested your code in Drupal 5.1 but it does not seem to work properly.
I've integrated the link module for the content type Story. Adding new link fields seems to work fine (the javascript part) but when pressing the submit button, I'm getting a blank page (no new story got created in consequence).

quicksketch’s picture

@Laurentvw I wouldn't say this code is ready for public consumption (the PHP portion). If you're getting a blank page it's probably because you're getting a PHP error with reporting turned off. This issue is mostly discussing the JS implementation though, not the PHP side.

@Moshe Thanks for the reference. I'll continue discussion over in groups. I'm leaving this issue open however because it accomplishes the same thing without an additional module. The two methods a fundamentally identical. ahah_forms simply uses one element to generate the necessary hidden fields, whereas here I'm making them manually.

yched’s picture

http://drupal.org/node/138905 was marked duplicate

quicksketch’s picture

I went ahead an put more.js into link.module 2.0 for those interested in trying it out. The dev site I posted above is down at the moment. Dopry and I might make greater plans for a core ahah.js file for Drupal 6. Which will include this functionality as well as the upload.js functionality.

moshe weitzman’s picture

Status: Needs review » Fixed

a different version of this is in HEAD of CCK

Anonymous’s picture

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.