Hi there,
I'm trying to do something that I was hoping would be relatively simple. My goal is just to programmatically select a single radio button in a custom CCK field.
Custom Content Type & Field
I've created a custom content type ("deposit_campaign_response") in CCK with a custom field. Here are the specs on the field:
- Field name: field_status
- Field type: Text
- Widget type: Check boxes/radio buttons
- Allowed values list:
- 1|Yes, I will deposit.
- 2|I'm pretty sure that I am coming.
- 3|I'm still thinking about it. I might need some more information.
- 4|No, I've made other plans.
- 5|I've already submitted my deposit.
Custom Module
I've created a custom module named "dcr". Here's the code that I've tried in order to select a specific option.
First Attempt:
First, I tried just a simple use of a form-specific form_alter function, like so. (I confirmed that the function was being run on the correct form.)
function dcr_form_deposit_campaign_response_node_form_alter(&$form, &$form_state) {
$form['field_status']['#default_value'][0]['value'] = 1;
return $form;
}
RESULT: The widget disappears completely from the $form array, and therefore doesn't display.
Second Attempt:
Next, I tried an "#after_build" function, like so (based on http://drupal.org/node/726282):
function dcr_form_deposit_campaign_response_node_form_alter(&$form, &$form_state) {
$form['#after_build'][] = 'dcr_after_build';
return $form;
}
function dcr_after_build($form, &$form_state) {
$form['field_status']['#default_value'][0]['value'] = 1;
return $form;
}
RESULT: No change.
Other Varied Attempts:
I also tried setting the keys to the option label, like
$form['field_status']['#default_value'][0]['value'] = t('Yes, I will deposit.');
As a last ditch effort, I tried going through every "value", "#value", or "#default_value" key in both the $form and $form_state arrays, but setting that didn't seem to help. Seems like I'm missing something simple or obvious.
Any ideas as to what I'm doing wrong?!
Comments
wild guess
but try making [0] into ['0']? Also I think 'value' should be '#value' but #default_value might be better.
Print out (print_r()) the contents of $form[] in your _alter function so you can see exactly what the variables are named.
www.purpleoar.co.nz/scryptik - Javascript editor with syntax error checking
www.purpleoar.co.nz - Web development, Drupal consultancy
I'm having the same problem
I'm having the exact problem and I can't find any solution. I manage to find the places the data from the selection is stored in $form and $form_state, but changing it there didn't make any change.
So have you found a solution yet?
hi, we can use
hi,
we can use hook_form_alter to that.