I'm trying to prepopulate/assign values on a node edit form for multigroup fields but it seems it doesn't work. I have a hook_form_alter where if I assign a values for a normal (not multigroupped) text field it works but for a textfield in a multigroup it doesn't seem to work.

I've attached a module for exemplification purposes. You just have to enable the module: it will automatically create a multigroup_prepop content type with 2 textfields. The module will also create a multigroup_prepop node type and it will display a message so you can easily locate the node. When you go the the edit page of the created node you'll see that the standalone textfield is assigned a value but it won't work for the multigroupped textfield.

Of course, for it to work it requires CCK 6.x-3.x-dev branch.

Thanks,
Andrei

Am I doing something wrong here or is it a multigroup/cck bug?

CommentFileSizeAuthor
multigroup_prepop.tar_.gz1.78 KBandreiashu

Comments

markus_petrux’s picture

Status: Active » Fixed

Your module needs to load after the fieldgroup module (changing the weight column in {system} table). To see why, we need to explore how the node form is built:

  1. The node module builds the node form.
  2. CCK implements hook_form_alter() to add CCK fields to the form and reorder top level elements in the form (fields and extra elements).
  3. The fieldgroup module has a weight of 9, so its hook_form_alter() executes when CCK itself has performed its own job. Here, the fieldgroup module adds top level elements to the form for each group (these are fieldset elements), and moves the field related elements CCK added to the form to their own fieldsets.
  4. Then, the fieldgroup module invokes hook_fieldgroup_form() for each field group, to allow external modules alter the field group related stuff in the form. Here's where the Multigroup module starts to play with the form.
  5. The multigroup removes the field elements from the fieldgroup and build their own elements because the cardinality of fields changes in a multigroup. Each subgroup of fields should render a widget for single value elements.

So, if you want to alter the field related elements in a multigroup, your module needs a weight higher than the one used by the fieldgroup module. In that way, your hook_form_alter() will be able to find the form with all the changes the fieldgroup + multigroup did already in place.

andreiashu’s picture

Hi Markus,

Thank you very much for the detailed explanation.

I changed the weight of my custorm module to something > 9 and everything works as expected now.

Thanks!

Status: Fixed » Closed (fixed)

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

seancorrales’s picture

Just wanted to say thanks for sharing not only how to do this, but why it works this way. This saved me a lot of agony! :)

-Sean

Josephnewyork’s picture

Good to know!

dafeder’s picture

FYI folks working on this issue, I was NOT able to set multiple default values for multigrouped fields via hook_form_alter. However, I WAS finally able to do it by setting the field's values with hook_nodeapi() on $op == prepare.