I'm trying to override the default value for a dropdown field.

In my module tester, I have

function teachhub_edit_profile_form_alter(&$form, &$form_state, $form_id) {
  
   if ($form_id == 'user_register_form') {

      $form["field_howdidyoufind"]["und"][0]["value"]['#default_value'] = "- Select -";

   }


}

This is not working. I tried replacing 0 with "_none" but that did not work either.

Comments

nevets’s picture

Default value needs to be the key value.

And that looks like a field added to the user object, in which case why not adjust the default setting for the field.

dsevcik’s picture

Here is the code below for the dropdown in Firebug: Would I change #default_value to "_none"?

- Select a value -
Online Search
Social Networking
TeachHUB Email
TeachHUB Newsletter
In-service Event
Word of Mouth
Other
dsevcik’s picture

With code:

<select id="edit-field-howdidyoufind-und" class="form-select required" name="field_howdidyoufind[und]">
<option value="_none">- Select a value -</option>
<option value="Online Search">Online Search</option>
<option value="Social Networking">Social Networking</option>
<option value="Email">TeachHUB Email</option>
<option value="Newsletter">TeachHUB Newsletter</option>
nevets’s picture

'_none' would be the correct value, though you should be able to set the default by configuring the field.

dsevcik’s picture

I did this and got a 500 error:

$form['field_howdidyoufind']['und'][0]['value']['_none'] = "- Select -";
nevets’s picture

How about trying

$form['field_howdidyoufind']['und'][0]['value'] = '_none';
dsevcik’s picture

From my paste above, the key for that field is already _none. I want to change the display value from the default of "Select a value" to just "Select".

dsevcik’s picture

Solved, I looped through the 2nd tier array and found it:

$form['field_howdidyoufind']['und']['#options']['_none'] = "- Select -";
raoofabdul’s picture

use LANGUAGE_NONE instead of 'und'

$form['field_howdidyoufind'][LANGUAGE_NONE]['#options']['_none'] = "- Select -";