I use multigroup in cck-6.x-3.x-dev .

in my case, i can not decide the number of times to repeat the collection of Multigroup fields in develope time, so i want to programmtically add more values in run time.

i use hook_form_alter(), but seems nothing i can do.

how to add values as many as needed in form processing ?

Comments

markus_petrux’s picture

Status: Active » Fixed

There's no easy way to do this because the complexity of the multigroup module itself, so you may need to understand what's going on.

The trick lives in $form_state['item_count'], which is an array used by the multigroup module to know how many items to build during "Add more items" processing.

Here's the basic recipe:

1) Your module needs to be loaded before the multigroup module (alter the weight column in {system} table for your module based on weight used by multigroup, use hook_enable).
2) Define the 'Number of repeats' option of your multigroup to unlimited.
3) Implement hook_fieldgroup_form() and alter the value of $form_state['item_count'][$group_name] to suit your needs.

The hook_fieldgroup_form() implementation of the multigroup will run after the one implemented by your module, and it will use the value you set there.

See node form related code in Multigroup module. Files content_multigroup.module and content_multigroup.node_form.inc

Status: Fixed » Closed (fixed)

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

3dloco’s picture

+1

jelle_s’s picture

When editing a node, the amount of values in a multigroup is equal to the amount of values entered for that node (if 'Number of repeats' option is set to unlimited). If you want to have e.g. 5 extra empty fields as standard (so you don't have to click 'add more values' 5 times) for a certain group, the trick is to do the following:
step 1 and 2 are the same as in #1, but in step 3, the implementation of hook_fieldgroup_form, you do this:

function MODULENAME_fieldgroup_form(&$form, &$form_state, $form_id, $group){
  if($group['group_name'] == $your_group_name){
    $form_state['item_count'][$your_group_name] = count($form['#node']->field_name) + NUMBER_OF_EXTRA_FIELDS;
  }
}

where field_name is the name of any field that is a member of your group and NUMBER_OF_EXTRA_FIELDS would be 5 in our example.