New module: Form API extension to create dynamic arrays of items

Events happening in the community are now at Drupal community events on www.drupal.org.
dmitriy.trt's picture

I've almost finished module to provide new Form API element (Dynarray), which generates dynamic elements array. For example, you want to have fieldset with Cars, each car has 3 fields (model, color, country) and you don't know number of cars, users should add as many cars as they want.

With dynarray it will be simple, you'll need to define form element:

<?php
$form
['cars'] = array(
 
'#type' => 'dynarray',
 
'#display_type' => 'fieldset', //Optional
 
'#title' => t('Cars'),
 
'#min' => 0, //Default minimum is 1, but we assume not all people have a car
 
'#max' => 5, //Limit cars number to 5
 
'#item' => array( //Single element definition, you can write ANY form elements here, even nested trees, fieldsets and even more dynarrays!
   
'#type' => 'fieldset',
   
'#title' => t('One car'),
   
'model' => array(
     
'#type' => 'textfield',
     
'#title' => t('Model'),
    ),
   
'color' => array(
     
'#type' => 'select',
     
'#title' => t('Color'),
     
'#options' => array(...),
    ),
   
'country' => array(
     
'#type' => 'textfield',
     
'#title' => t('Country'),
    ),
  ),
);
?>

This will result one fieldset with button 'Add', which adds more elements through AHAH, all elements (except those shouldn't be deleted according to #min property) will have button 'Delete'.
You will be able to define #default_value array and there will be elements created and filled with this default values.
Also, it will be possible to define #item_generator property with function (default_value will be an argument for this function), which should return Form API definition of one item.

Am I writing a duplicate of existing module? Is there anyone working on similar modules?...

Comments

Not exactly the same, but

smk-ka's picture

Not exactly the same, but have you taken a look at the Multigroup module which is part of CCK 3.x? It allows pretty much the same, just without coding, i.e. everything is done with fields and within the CCK UI.

-Stefan

Thanks, I'll take a look at

dmitriy.trt's picture

Thanks, I'll take a look at it.

What Dmitriy is working on is

SamRose's picture

What Dmitriy is working on is still needed, for use cases (and there are many) where people want to use form api, and do not want to cck...

After some experimentation, I

SamRose's picture

After some experimentation, I realized http://drupal.org/project/ahah_helper (as of this writing version DRUPAL-6--2 in cvs) definitely does this via form api.

An example that I was able to get working is here: http://jamestombs.co.uk/2010-07-08/adding-additional-form-elements-using...

Contributed Module Ideas

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: