The only reference I could find to this problem is an 18-month-old post in the forums. I can't get my CCK select list to accept a default value specified by hook_form_alter.

My field type is an integer, select list. I've put a bunch of default values in to mimick the usual Drupal 'weight' fields (from 10 to -10), so I want '0' to be the default. Here's what I've tried, none of which have any effect on the form:

//value as a string
$form['field_faq_weight']['key']['#default_value'] = '0';

//value as an integer, just in case!
$form['field_faq_weight']['key']['#default_value'] = 0;

//value as an array
$form['field_faq_weight']['key']['#default_value'] = array(0 =>0);

I then found this thread: http://drupal.org/node/91759
I tried an integer which would represent a key in the range of my array if it were autonumbered:

//value as a valid auto-numbered key
$form['field_faq_weight']['key']['#default_value'] = 2;

Still nothing! My $form array looks like this at the moment, which ought to do *something*:

    [field_faq_weight] => Array
            [#tree] => 1
            [key] => Array
                    [#type] => select
                    [#title] => FAQ Weight
                    [#default_value] => 2
                    [#multiple] =>
                    [#options] => Array
                            [10] => 10
                            [9] => 9
                            [8] => 8
                            [7] => 7
                            [6] => 6
                            [5] => 5
                            [4] => 4
                            [3] => 3
                            [2] => 2
                            [1] => 1
                            [0] => 0
                            [-1] => -1
                            [-2] => -2
                            [-3] => -3
                            [-4] => -4
                            [-5] => -5
                            [-6] => -6
                            [-7] => -7
                            [-8] => -8
                            [-9] => -9
                            [-10] => -10

                    [#required] => 1
                    [#description] =>

            [#weight] => -1

My default value is having absolutely no effect on the select list no matter what I do. Why is this?




James Marks’s picture

The short answer is that your array of select options needs to be an associative array consisting of a $key => $value pairs where both the $key and $value contain the option value.

If you hand an indexed array of options to a select list, it will produce select options that use the index as the value. For example, this:

= array('Red', 'Green', 'Blue');

$form['example']['colors'] = array(
'#title' => t('Colors'),
'#type' => 'select',
'#options' => $options,
'#default_value' => 'Red',

will produce this:

<select name="colors" class="form-select" id="edit-colors" >
option value="0"></option>
option value="1">Red</option>
option value="2">Green</option>
option value="3">Blue</option>

The '#default_value' of 'Red' isn't in the list of values so it won't display 'Red' as the selected choice.

The correct way to do this is to set an associative array of options so that this:

= array('Red' => 'Red', 'Green' => 'Green', 'Blue' => 'Blue');

$form['example']['colors'] = array(
'#title' => t('Colors'),
'#type' => 'select',
'#options' => $options,
'#default_value' => 'Red',

will produce this:

<select name="colors" class="form-select" id="edit-colors" >
option value=""></option>
option value="Red" selected="selected">Red</option>
option value="Green">Green</option>
option value="Blue">Blue</option>

The 'Red' option will be marked as 'selected' because the '#default_value' of 'Red' is in the list of values and can therefore be set as 'selected'.

Hope that helps.

greg.harvey’s picture

Status:Active» Closed (fixed)

Wow, that was a long time ago, but thanks for the detailed reply. It will help someone, for sure. =)

dapperry’s picture

How do you do this in code? I have a module where I read the CCK form in, and then (if user is logged in), prefill the fields with the users content profile. This works fine for textfields, but for the life of me I have not figured out how to set default_value for select.

function orderonline_form ($form_state) {

// Load in the Online Order form elements
$nodeType = 'online_order';
$form_id = $nodeType . '_node_form';

$node = array('type' => $nodeType, 'uid' => $GLOBALS['user']->uid, 'name' => $GLOBALS['user']->uid);
$form = drupal_retrieve_form('online_order_node_form',$form_state,$node);

// If the User is logged in, and has a content profile, we will preload the order form for them
$userid = $GLOBALS['user']->uid;
if ($userid > 0) {
$view = views_get_view('getCustomerProfileByUID');

if (count($view->result)) {
//This doesnt work as it is a select - eg Mr., Mrs. Ms, Dr., etc.
$form['group_aboutyou']['field_custprefix']['0']['#default_value']['value'] = $view->result[0]->node_data_field_custprefix_field_custprefix_value,);
//These work as they are textfields
$form['group_aboutyou']['field_custfirstname']['0']['#default_value']['value'] = $view->result[0]->node_data_field_custfirstname_field_custfirstname_value;
$form['group_aboutyou']['field_custmi']['0']['#default_value']['value'] = $view->result[0]->node_data_field_custmi_field_custmi_value;
$form['group_aboutyou']['field_custlastname']['0']['#default_value']['value'] = $view->result[0]->node_data_field_custlastname_field_custlastname_value;
$form['group_aboutyou']['field_custsuffix']['0']['#default_value']['value'] = $view->result[0]->node_data_field_custsuffix_field_custsuffix_value;
$form['group_aboutyou']['field_custtitle']['0']['#default_value']['value'] = $view->result[0]->node_data_field_custtitle_field_custtitle_value;
$form['group_aboutyou']['field_custorganization']['0']['#default_value']['value'] = $view->result[0]->node_data_field_custorganization_field_custorganization_value;


//echo "<PRE>";
//echo "</PRE>";
return $form;

lockev3.0’s picture

Title:Can't set default values on select list» Sorry for being late but ...
Version:5.x-1.6-1» 6.x-2.1

Sorry for being late but ...
That associative array does not seem a good way to refer selectable options.
What if I have something like:

colors ('High Red', 'Light grey', ......);

The expected here would be to index colors such as a database model would do :
(1=>'High Red', 'Light grey', ... )

In order to present the text in the select element but choose it thru its index.
And as you say, this ain't valid ? (indeed default_value it's not working for me)

KarenS’s picture

Version:6.x-2.1» 5.x-1.6-1

Let's don't resurrect a two year old issue for another version and try to make any sense out of it. If you have a question about the current code create a new issue with enough detail to tell what you're talking about. No clue what your question is.

nevets’s picture

Title:Sorry for being late but ...» Can't set default values on select list

Please do not change subject and version (I reset them). You should really post a new issue, not use an old closed one.

dreamsz71’s picture

Drupal 7 Form API select element
case: how to set default_value for a select form element (month field) so that Oct is selected as defult ?
step 1: for eg: $month=10;

step 2: $monthOptions=array("1"=>"january",
Step 3:
'#type'=> 'select',
'#title' => 'Month',
'#required' => 'true',
'#options' => $monthOptions,
'#default_value' => $month,


prsnjtbarman’s picture

#7 is correct