When adding a node based on a CCK type i want to change the default value in the select list based on a value in the current users profile.
Using hook_form_alter() this is easy for other fields, but the select field appears to ignore whatever #default_value i set.
Example, I have a list of states that are selectable in a CCK node form.
[field_state] => Array
(
[#tree] => 1
[key] => Array
(
[#type] => select
[#title] => State
[#default_value] => AL
[#multiple] =>
[#options] => Array
(
[1] => AK
[2] => AL
[3] => AR
[4] => AZ
[5] => CA
[6] => CO
[7] => CT
[8] => DE
[9] => FL
[10] => GA
[11] => HI
[12] => IA
[13] => ID
[14] => IL
[15] => IN
[16] => KS
[17] => KY
[18] => LA
[19] => MA
[20] => MD
[21] => ME
[22] => MI
[23] => MN
[24] => MO
[25] => MS
[26] => MT
[27] => NC
[28] => ND
[29] => NE
[30] => NH
[31] => NJ
[32] => NM
[33] => NV
[34] => NY
[35] => OH
[36] => OK
[37] => OR
[38] => PA
[39] => RI
[40] => SC
[41] => SD
[42] => TN
[43] => TX
[44] => UT
[45] => VA
[46] => VT
[47] => WA
[48] => WI
[49] => WV
[50] => WY
)
[#required] => 1
[#description] =>
)
)
Based on a state value in a users profile i then want to default the select menu to the same value. So in hook_form_alter() I override the $form with:
$form['field_state']['key']['#default_value'] = $user_data->state;
Using dprint_r() i can see that the #default_value is getting changed as expected in the $form, yet the select widget still defaults to the first item in the select list no matter what.
Also, why is the select widget $form array structure different to other fields such as testfields which have a [0][value] rather than [key] element in the array?
Comments
Setting the default value as
Setting the default value as an array doesn't work either.
--
Ixis (UK): Drupal support, Drupal hosting.
Experiencing the same problem
I'm seeing this problem too...
Did you find any solutions?
Update:
I seem to have got things working by setting
$form[$field]['key']['#default_value'] = $key
E.g. for an array:
[1] = A
[2] = B
$key = 1 or 2...
Sorry I never posted a
Sorry I never posted a follow-up to my query, but yes, you need to specify the key as the default, value, even when you #options don't define keys, they are being auto numbered by php anyway.
--
Ixis (UK): Drupal support, Drupal hosting.
Any advance?
Hi,
I know this is a very old post, but I still can't get this to work in the current version of CCK for Drupal 5.7. I have tried the above and it doesn't work - I've tried an array, a key number as though it were autonumber, as type of integer, string, nothing!! =(
My default value is having absolutely no effect on the select list.
--
http://www.drupaler.co.uk/
--
https://www.codeenigma.com/
Use PHP Code
If you want the default value being "0", just put this in the "Php Code" Textarea of your CCk Field (beneath the "Allowed values list"):
Now, if you create new Content, the default value is 0. (Don't know why, but it works for me..).
Thanks
Decent workaround! Thank you. =)
--
http://www.drupaler.co.uk/
--
https://www.codeenigma.com/