drupal_rebuild_form changes #default_values array structure in cck multiple checkbox

msoler75 - February 16, 2009 - 12:17
Project:AHAH helper
Version:6.x-2.0
Component:Code
Category:bug report
Priority:normal
Assigned:Unassigned
Status:active
Description

Hi, I think it is another issue. I dont' know is ahah helper or core bug.

I'm using hook_form_alter, in a form with cck multiple checkbox field:

<?php
function mymodule_form_alter(&$form, $form_state, $form_id) {
   switch(
$form_id) {
   case
'cck_node_form':                   
     
ahah_helper_register($form, $form_state);
     
//mymodule_alter_form($form, $form_state);
     
dsm($form['field_cck']['#default_value']);
      break;
   }
}
?>

Note the commented line: I don't modify the form;
The ahah_helper_register set storage value in $form, then, drupal_get_form calls drupal_rebuild_form. The result is that the mymodule_form_alter is called 2 times.
I use dsm function from devel module to show the $from values.

The first dsm call, the $form['field_cck']['#default_value'] is an array
[0] => {['value'] => 'item1'}
[1] => {['value'] => 'item2'}
[2] => {['value'] => 'item3'}
...

The second dsm call, the $form['field_cck']['#default_value'] is an array
[value] => {'item1' => TRUE, 'item2' => TRUE, 'item3' => TRUE, ...}

The final result is that when form is shown, the values in checkboxes are not filled.
For your info, the cck field type is #type optionwidgets_buttons.

Is this a cck or core bug...?

Can I avoid the drupal_rebuild_form calling?

I must follow another way to do this?

Thanks for your patience.

#1

getluky - June 2, 2009 - 01:12

I ran into this issue with a CCK custom date select field - it would blow out both default values and occasionally the date ranges on the second run through the form builder. Not sure why. Easy workaround was to modify ahah_helper.module as follows:

--- ahah_helper.module 2009-06-01 18:08:13.000000000 -0700
+++ ahah_helper.module.modified 2009-06-01 18:08:11.000000000 -0700
@@ -77,7 +77,7 @@
   $form['#cache'] = TRUE;

   // Register the file.
-  if (!isset($form_state['storage']['#ahah_helper']['file'])) {
+  if (isset($form_state['storage']) && !isset($form_state['storage']['#ahah_helper']['file'])) {
     $menu_item = menu_get_item();
     if ($menu_item['file']) {
       $form_state['storage']['#ahah_helper']['file'] = $menu_item['file'];

This prevents it from unnecessarily modifying the storage array when it doesn't even exist yet, which triggers the unnecessary rebuild. Not sure what side effects this might have, so YMMV.

#2

grahamgilchrist - August 3, 2009 - 10:06

Thanks very much for this.
I had a similar situation using form_alter on the user_register form in the same way as you. Whenever I ran ahah_helper_register() it duplicated the form either from cache or by rebuilding or something such that it had the wrong form id (user-register-1 instead of user_register). Adding your code did the trick to solve my problem too.

Any chance this can be committed if it doesn't break anything else?

Cheers

 
 

Drupal is a registered trademark of Dries Buytaert.