Hi,
I would like to change a cck field checkboxes/unlimited to radio checkboxes using hook form alter and need help (I hope this will not cost too much). I am needing to do with hook form alter because I want it different depending on the user's role. If the user has examplerole I want it to be radio, but if user is just authenticated then I want the field to be checkboxes/unlimited.
This is what I'm trying in a custom module but getting WSOD =(
function mymodule_form_alter(&$form, $form_state, $form_id) {
switch ($form_id) {
case 'profile_node_form':
global $user;
if(in_array('examplerole', $user->roles)) {
$form['field_example']['value']['#multiple'] = 0;
$form['field_example']['value']['#type'] = 'radios';
}
break;
}
}
note: the cck content type is called 'profile'
Using print_r($form) this is what is under $form
[field_example] => Array
(
[#type] => optionwidgets_buttons
[#default_value] => Array
(
[0] => Array
(
[value] => ??
)
[1] => Array
(
[value] => ??
)
[2] => Array
(
[value] => ??
)
)
[#required] => 0
[#columns] => Array
(
[0] => value
)
[#title] => ???????
[#description] =>
[#delta] => 0
[#field_name] => field_example
[#type_name] => profile
[#tree] => 1
[#weight] => 23
[#access] => 1
[#count] => 9
[#post] => Array
(
)
[#programmed] =>
[#parents] => Array
(
[0] => field_example
)
[#array_parents] => Array
(
[0] => field_example
)
[#processed] => 1
[#attributes] => Array
(
)
[#input] => 1
[#process] => Array
(
[0] => optionwidgets_buttons_process
)
[#name] => field_example
[#id] => edit-field-example
[#value] => Array
(
[value] => Array
(
[0] => ??
[1] => ??
[2] => ??
)
)
[value] => Array
(
[#type] => checkboxes
[#title] => ???????
[#description] =>
[#required] => 0
[#multiple] => 1
[#options] => Array
(
[??] => ??
[??] => ??
[??] => ??
)
[#default_value] => Array
(
[0] => ??
[1] => ??
[2] => ??
)
[#post] => Array
(
)
[#programmed] =>
[#tree] => 1
[#parents] => Array
(
[0] => field_example
[1] => value
)
[#array_parents] => Array
(
[0] => field_example
[1] => value
)
[#weight] => 0
[#processed] => 1
[#attributes] => Array
(
)
[#input] => 1
[#process] => Array
(
[0] => expand_checkboxes
)
[#name] => field_example[value]
[#id] => edit-field-example-value
[#value] => Array
(
[??] => 1
[??] => 1
[??] => 1
)
[??] => Array
(
[#type] => checkbox
[#processed] => 1
[#title] => ??
[#return_value] => ??
[#default_value] => 1
[#attributes] => Array
(
)
[#ahah] =>
[#post] => Array
(
)
[#programmed] =>
[#tree] => 1
[#parents] => Array
(
[0] => field_example
[1] => value
[2] => ??
)
[#array_parents] => Array
(
[0] => field_example
[1] => value
[2] => ??
)
[#weight] => 0
[#description] =>
[#required] =>
[#input] => 1
[#process] => Array
(
[0] => form_expand_ahah
)
[#name] => field_example[value][??]
[#id] => edit-field-example-value-??
[#value] => 1
[#defaults_loaded] => 1
[#sorted] => 1
)
[??] => Array
(
[#type] => checkbox
[#processed] => 1
[#title] => ??
[#return_value] => ??
[#default_value] => 1
[#attributes] => Array
(
)
[#ahah] =>
[#post] => Array
(
)
[#programmed] =>
[#tree] => 1
[#parents] => Array
(
[0] => field_example
[1] => value
[2] => ??
)
[#array_parents] => Array
(
[0] => field_example
[1] => value
[2] => ??
)
[#weight] => 0.001
[#description] =>
[#required] =>
[#input] => 1
[#process] => Array
(
[0] => form_expand_ahah
)
[#name] => field_example[value][??]
[#id] => edit-field-example-value-??
[#value] => 1
[#defaults_loaded] => 1
[#sorted] => 1
)
[??] => Array
(
[#type] => checkbox
[#processed] => 1
[#title] => ??
[#return_value] => ??
[#default_value] => 1
[#attributes] => Array
(
)
[#ahah] =>
[#post] => Array
(
)
[#programmed] =>
[#tree] => 1
[#parents] => Array
(
[0] => field_example
[1] => value
[2] => ??
)
[#array_parents] => Array
(
[0] => field_example
[1] => value
[2] => ??
)
[#weight] => 0.002
[#description] =>
[#required] =>
[#input] => 1
[#process] => Array
(
[0] => form_expand_ahah
)
[#name] => field_example[value][??]
[#id] => edit-field-example-value-??
[#value] => 1
[#defaults_loaded] => 1
[#sorted] => 1
)
[#defaults_loaded] => 1
[#sorted] => 1
)
[#element_validate] => Array
(
[0] => optionwidgets_validate
)
[#defaults_loaded] => 1
[#sorted] => 1
)
note: i just replaced the values with ?? for above
Also, is it ok to change the field in this way for the backend database (having it multiple for some users and radio for others?)
Thanks!
Comments
Try writing your
Try writing your 'hook_form_alter' function as below:
hi ravi_sam, I just tried
hi ravi_sam,
I just tried your code above, no WSOD but it still shows the 3 options as checkboxes on the profile node form and multiple can be selected. Also, I took out the if(in_array('examplerole', $user->roles)) part for testing but still the same =(
Slightly different approach
How about creating 2 fields in the 'manage field' of this content type, one as radios and the other as checkboxes? Then the hook_form_alter will be:
and the profile template should be coded to display the appropriate field based on the role.
Are you asking for checkboxes
Are you asking for checkboxes that only allow one thing to be selected? I don't think that's possible without resorting to javascript+server side validation. Even then, it's non-standard behavior for checkboxes.
http://www.trailheadinteractive.com
solution:function
solution:
thanks s13 and ravi_sam for your help!
Wow, super simple! I like
Wow, super simple! I like it!
http://www.trailheadinteractive.com