0
  • 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:

<?php
function createFieldMap(){
    $fieldMapping = array(
        "LastName" => "field_last_name",
        "FirstName" => "field_first_name",
        "Salutation" => "field_prefix"
    );
}
function custom_module_form_commerce_checkout_form_registration_alter(&$form, &$form_state, $form_id) {
    $fieldMap = createFieldMap();
    foreach ($form['registration_information']['register_entities'] as $name => $data) {
        foreach($form['registration_information'][$name] as $subname => $subdata) {
            foreach ($fieldMap as $salesforceKey => $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.

  • I have no idea of commerce / Salesforce, and form alters aren't easy for me, but in general, I thought text fields should have a language.. Usually it's like Language_None or something. See drupal.stackexchange.com/questions/25839/… Edit: Just saw you fixed it. The und is the language part of it – Niall Murphy Nov 9 '17 at 20:24
  • Thanks Niall. I did try before using und/LANGUAGE_NONE but still couldn't get it to work, but noticed it was deeper down the array tree that I needed to go. oh nesting.. – Travis Johnston Nov 9 '17 at 20:29
0

Fixed it. I had to work with ['und'][0]['value']['#default_value']

So my new code is:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.