Prefill Registration Form Fields Entity Registration 
Commerce Registration 
Drupal 7


Our site connects with Salesforce to input Entity Registration data as Contacts. I'm building in the ability to have users login and have their registration forms prefilled with the data I grab from Salesforce. 

I have the data, but the prefill isn't working. Commerce Registration buries the forms a bit, as you will see below in my code example. I decided to step out of using the Salesforce data and just use a static value and try to set it. Both default_value and placeholder do nothing. If I try to touch ['value'] the site crashes. 

Here is the trimmed down version of what I have:

&lt;?php
function createFieldMap(){
    $fieldMapping = array(
        "LastName" =&gt; "field_last_name",
        "FirstName" =&gt; "field_first_name",
        "Salutation" =&gt; "field_prefix"
    );
}
function custom_module_form_commerce_checkout_form_registration_alter(&amp;$form, &amp;$form_state, $form_id) {
    $fieldMap = createFieldMap();
    foreach ($form['registration_information']['register_entities'] as $name =&gt; $data) {
        foreach($form['registration_information'][$name] as $subname =&gt; $subdata) {
            foreach ($fieldMap as $salesforceKey =&gt; $drupalMachine) {
                if($drupalMachine == 'field_last_name'){
                   $form['registration_information'][$name][$subname]['field_last_name']['#default_value'] = "Johnston";
                   $form['registration_information'][$name][$subname]['field_last_name']['#attributes']['placeholder'] = t("Johnston");
               }
            }
        }
    }
}


Just to point out, $form['registration_information'][$name][$subname] is valid and is done that way because if you have multiple forms, the productID is used as the form ID, twice. I can access $form['registration_information'][$name][$subname]['field_last_name'] if I var_dump it out. ['value'] represents entity data for some reason, which is probably why it crashes if I try to adjust that. 

I can get this to work fine by setting the default_value of select lists. But the text fields don't do anything.
Fixed it. I had to work with
['und'][0]['value']['#default_value']

So my new code is:

&lt;?php
    $fieldMap = createFieldMap();
    foreach ($form['registration_information']['register_entities'] as $name =&gt; $data) {
        foreach($form['registration_information'][$name] as $subname =&gt; $subdata) {
            foreach ($fieldMap as $salesforceKey =&gt; $drupalMachine) {
                $form['registration_information'][$name][$subname][$drupalMachine]['und'][0]['value']['#default_value'] = $contact[$salesforceKey];                                 
            }
        }
    }