Community

drupal_get_form seemingly caching extra passed arguments

I have been unable to solve this issue for several days now, and I don't know where else to turn.

Hopefully the below relations should be pretty self-explanatory. .. In case they are not, '_mwadmin_admin_act_inact_table_gen()' generates two tables and returns them in an array. 'mwadmin_contents()' simply returns a query object for a given table alias.

What currently happens doesn't make much sense in my book, but I'm still learning this API so I'm pretty sure I've missed something important... anyway, when the 'sub form' is submitted, it regenerates the tables and sends them to a custom js command. The only problem is that the data present in the regenerated tables is not right. While the regen'd checkboxes and buttons have the correct values.. the regen'd sub-form does not, and actually contains the value of the last submitted sub-form. I can't figure out why it's doing this.. if any more information is needed, I will do my best to provide.

<?php
function _mwadmin_admin_act_inact_table_gen() {
   
$user = $GLOBALS['user'];
   
$no_yes = array('No','Yes');
   
   
$admin_cur_act = mwadmin_contents('admin_cur_act');
   
//Array to contain items for the page to render.
   
$admin_cur_act_items = array();
   
    foreach (
$admin_cur_act as $node) {
       
$sub_form = drupal_get_form('mwadmin_delete_sub_form',$node->sid);
       
        if (
$node->upgrade_request != '') {
           
$upgrade_request = 1;
        }
        else {
           
$upgrade_request = 0;
        }
       
       
$admin_cur_act_items[] = array(
           
'data' => array(
                array(
'data' => theme('checkbox', array(
                   
'element' => array(
                       
'#attributes' => array(
                           
'class' => array('mwadmin_active_sel'),
                           
'data-sid' => $node->sid
                       
)
                    )
                ))),
                array(
'data' => $user->name),
                array(
'data' => unserialize($node->info)[0]),
                array(
'data' => $node->type),
                array(
'data' => $no_yes[$upgrade_request]),
                array(
                   
'data' => l(t('View'), 'mwadmin/sheet', array('query' => array('mode' => 'view', 'sid' => $node->sid)))
                    .
' / ' . l(t('Edit'), 'mwadmin/sheet', array('query' => array('mode' => 'admin_edit', 'sid' => $node->sid)))
                ),
                array(
                   
'data' => theme('checkbox', array(
                           
'element' => array(
                               
'#attributes' => array(
                                   
'class' => array('mwadmin_del_chk'),
                                )
                            )
                        )) .
' ' . theme('button', array(
                           
'element' => array(
                               
'#button_type' => 'button',
                               
'#value' => 'Delete',
                               
'#attributes' => array(
                                   
'class' => array(
                                       
'mwadmin_del_but',
                                       
'form-submit',
                                       
'form-button-disabled',
                                    ),
                                   
'type' => 'button',
                                   
'disabled' => 'TRUE',
                                   
'data-sid' => $node->sid
                               
)
                            )
                        )) .
'<br />' . render($sub_form)
                       
//drupal_get_form('mwadmin_delete_sub_form',$node->sid),
                       
               
)
            )
        );
    }
    if (empty(
$admin_cur_act_items)) { //No content
       
$admin_cur_act_table['mwadmin_tbl_admin_cur_act'] = array(
           
'#theme' => 'table__mwadmin',
           
'#caption' => t('Current active sheets'),
           
'#header' => array('Select','Owner','Character','Type','Upgrade Request','View/Edit','Delete'),
           
'#empty' => t('No sheets available.'),
           
'#attributes' => array(
               
'id' => 'mwadmin_tble_admin_cur_act'
           
)
        );
    }
    else {
       
$admin_cur_act_table['mwadmin_tbl_admin_cur_act'] = array(
           
'#theme' => 'table__mwadmin',
           
'#caption' => t('Current active sheets'),
           
'#header' => array('Select','Owner','Character','Type','Upgrade Request','View/Edit','Delete'),
           
'#rows' => $admin_cur_act_items,
           
'#attributes' => array(
               
'id' => 'mwadmin_tble_admin_cur_act'
           
)
        );
    }
   
    ...

    return array(
       
$admin_cur_act_table,
       
$admin_cur_inact_table
   
);
}

function
mwadmin_delete_sub_form($form, &$form_state, $sid) {
   
   
$form['#attributes'] = array(
       
'id' => 'mwadmin-delete-sub-form--' . $sid
   
);
   
   
$form['mwadmin_delete_sub'] = array(
       
'#type' => 'hidden',
       
'#value' => $sid,
    );
   
   
$form['mwadmin_delete_sub_but'] = array(
       
'#type' => 'button',
       
'#ajax' => array(
           
'callback' => 'mwadmin_delete_sub_callback',
        ),
       
'#attributes' => array(
           
'data-id' => 'mwadmin_delete_sub_' . $sid,
           
'style' => 'display: none;',
        )
    );
   
    return
$form;
}

function
mwadmin_delete_sub_callback(&$form, $form_state) {
   
   
db_update('mwadmin_current_sheets')
        ->
fields(array(
           
'active' => 0
       
))
        ->
condition('sid', $form_state['values']['mwadmin_delete_sub'])
        ->
execute();
   
   
$table_arr = _mwadmin_admin_act_inact_table_gen();
   
    return array(
       
'#type' => 'ajax',
       
'#commands' => array(
           
mwadmin_command_admintables($table_arr[0],$table_arr[1])
        )
    );
}
?>

Comments

mwadmin_delete_sub_callback()

mwadmin_delete_sub_callback() should not modify form elements, all modifications to form elements should be in the form function its self.

I'm a tad confused,

I'm not seeing where it does that. I'm sorry, but could you be a little more specific?

This code does more than

This code does more than return an existing form element (or set of elements)

    $table_arr = _mwadmin_admin_act_inact_table_gen();
  
    return array(
        '#type' => 'ajax',
        '#commands' => array(
            mwadmin_command_admintables($table_arr[0],$table_arr[1])
        )
    );

and would appear to change form elements. Those changes since they are outside the form function would not be reflected in the saved form.

As far as I am aware,

As far as I am aware, those changes are not outside the form function. The new $sid value is passed directly to the form function, which if I understand it correctly, is supposed to regenerate the form. If I'm still not understanding, I apologize.

Yes they are, the callback

Yes they are, the callback form should not modify the form, all changes need to be in the form function.

Any suggestions?

Then do you have any suggestions on how I am supposed to regenerate the tables? I can't really see a way around this.

Add the tables as a form

Add the tables as a form element in the form function (you can always use 'markup') as the type and have the ajax callback return that form element.

That doesn't seem viable,

Given I have to have separate forms for each row(different sid and such), that doesn't appear to be viable.

nobody click here