Community

Drupal Default Value for Dropdown field

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

In my module tester, I have

<?php
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

Default value needs to be the

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.

Here is the code below for

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

With code: <select

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>

'_none' would be the correct

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

I did this and got a 500

I did this and got a 500 error:

<?php
$form
['field_howdidyoufind']['und'][0]['value']['_none'] = "- Select -";
?>

How about

How about trying

$form['field_howdidyoufind']['und'][0]['value'] = '_none';

From my paste above, the key

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".

Solved, I looped through the

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

<?php
$form
['field_howdidyoufind']['und']['#options']['_none'] = "- Select -";
?>