This post is the second part of Create Drupal form using theme_table() like module list form post. In first part I have showed how we can create Drupal form like we have in Module list pages. In this post, I am showing how we disable some of the checkboxes so user can’t select them like we have for core modules in module list page. We can’t enable/disable core modules in module list.

Drupal Form using theme_table() function (Part II)
Here I am just showing the changes which we require in code of Part I post.
$form['featured'] = array( '#type' => 'checkboxes', '#options' => $options, '#default_value' => $status, ); // Change this to $form['featured'] = array( '#type' => 'checkboxes', '#options' => $options, '#default_value' => $status, '#process' => array( 'expand_checkboxes', 'featured_disable', ), '#disabled_products' => $disabled, );
In above code “$disabled” is an array containing the productid’s of all the products which we want to show as checked and in disable state. The structure of “$disabled” array is similar to “$status” array. We stored this array in “#disabled_products”. We also added “#process” in above code.
“expand_checkboxes” and “featured_disable” are function names which Drupal call when create and render these checkboxes. “expand_checkboxes()” is defined in Drupal core files and we don’t need to define it again.
function featured_disable($form, $edit) { foreach ($form['#disabled_products'] as $key) { $form[$key]['#attributes']['disabled'] = 'disabled'; } return $form; }
“featured_disable()” function is very important here. Drupal call this function when renders these checkboxes. Here we assigned “disabled” attribute to each checkbox which we want as disable.
#1 by Tyler - October 2nd, 2009 at 20:33
Thanks for the tutorial – if you’d call it that. The first part has characters that need to be edited for it to even work. Not to mention there are parts where you are absolutely unclear about what you should do. Thanks for taking the time though.
#2 by admin - October 2nd, 2009 at 23:05
Thanks Tyler for your comment. Can you please let me know which part of this tutorial is not working so we can fix it.
Thanks,
Anil
#3 by masslamet - July 1st, 2010 at 07:22
I just tried this but when i ran it, there was warning: Invalid argument supplied for foreach() in , “foreach ($form['#disabled_products'] as $key) {”
how solve it?
thanks.
#4 by admin - July 5th, 2010 at 11:39
@ masslamet
Can you confirm the value of $disabled array you used in this line.
‘#disabled_products’ => $disabled,
The $disabled should be an array with values/checkbox you want to disable.