1

I would like to set the first and last name field using hook form alter. I am already in the right form and able to dump the array you can see it below. This is the form 'commerce_addressbook_customer_profile_form':

The field I want to populate is this

https://screencast.com/t/OUkrOtpclfC

I am unable to set the value of the first and last name field. Here is the array when I print $form.

  Array
(
    [#parents] => Array
        (
        )

    [#entity] => stdClass Object
        (
            [type] => shipping
            [uid] => 1481
            [profile_id] => 
            [revision_id] => 
            [status] => 1
            [created] => 
            [changed] => 
            [is_new] => 1
            [_is_new] => 1
            [name] => ASDFASDF_1481
            [date] => 2017-02-06 23:04:06 -0500
        )

    [commerce_customer_address] => Array
        (
            [#type] => container
            [#attributes] => Array
                (
                    [class] => Array
                        (
                            [0] => field-type-addressfield
                            [1] => field-name-commerce-customer-address
                            [2] => field-widget-addressfield-standard
                        )

                )

            [#weight] => -10
            [#tree] => 1
            [#language] => und
            [und] => Array
                (
                    [0] => Array
                        (
                            [#entity_type] => commerce_customer_profile
                            [#entity] => stdClass Object
                                (
                                    [type] => shipping
                                    [uid] => 1481
                                    [profile_id] => 
                                    [revision_id] => 
                                    [status] => 1
                                    [created] => 
                                    [changed] => 
                                    [is_new] => 1
                                    [_is_new] => 1
                                    [name] => ASDFASDF_1481
                                    [date] => 2017-02-06 23:04:06 -0500
                                )

                            [#bundle] => shipping
                            [#field_name] => commerce_customer_address
                            [#language] => und
                            [#field_parents] => Array
                                (
                                )

                            [#columns] => Array
                                (
                                    [0] => country
                                    [1] => administrative_area
                                    [2] => sub_administrative_area
                                    [3] => locality
                                    [4] => dependent_locality
                                    [5] => postal_code
                                    [6] => thoroughfare
                                    [7] => premise
                                    [8] => sub_premise
                                    [9] => organisation_name
                                    [10] => name_line
                                    [11] => first_name
                                    [12] => last_name
                                    [13] => data
                                )

                            [#title] => Address
                            [#description] => 
                            [#required] => 1
                            [#delta] => 0
                            [#weight] => 0
                            [#type] => fieldset
                            [#handlers] => Array
                                (
                                    [0] => address
                                    [5] => name-full
                                )

                            [street_block] => Array
                                (
                                    [#type] => addressfield_container
                                    [#attributes] => Array
                                        (
                                            [class] => Array
                                                (
                                                    [0] => street-block
                                                )

                                        )

                                    [#weight] => 0
                                    [thoroughfare] => Array
                                        (
                                            [#title] => Address 1
                                            [#tag] => div
                                            [#attributes] => Array
                                                (
                                                    [class] => Array
                                                        (
                                                            [0] => thoroughfare
                                                        )

                                                    [autocomplete] => address-line1
                                                )

                                            [#size] => 30
                                            [#maxlength] => 255
                                            [#required] => 1
                                        )

                                    [premise] => Array
                                        (
                                            [#title] => Address 2
                                            [#tag] => div
                                            [#attributes] => Array
                                                (
                                                    [class] => Array
                                                        (
                                                            [0] => premise
                                                        )

                                                    [autocomplete] => address-line2
                                                )

                                            [#size] => 30
                                            [#maxlength] => 255
                                        )

                                )

                            [locality_block] => Array
                                (
                                    [#type] => addressfield_container
                                    [#attributes] => Array
                                        (
                                            [class] => Array
                                                (
                                                    [0] => addressfield-container
                                                    [1] => locality-block
                                                    [2] => country-GB
                                                )

                                        )

                                    [#weight] => 50
                                    [#maxlength] => 255
                                    [#attached] => Array
                                        (
                                            [css] => Array
                                                (
                                                    [0] => sites/all/modules/addressfield/addressfield.css
                                                )

                                        )

                                    [dependent_locality] => Array
                                        (
                                            [#title] => Suburb
                                            [#required] => 
                                            [#access] => 
                                            [#size] => 25
                                            [#maxlength] => 255
                                            [#tag] => div
                                            [#attributes] => Array
                                                (
                                                    [class] => Array
                                                        (
                                                            [0] => dependent-locality
                                                        )

                                                    [autocomplete] => address-level3
                                                )

                                            [#suffix] => 

                                        )

                                    [locality] => Array
                                        (
                                            [#title] => Town/City
                                            [#required] => 1
                                            [#access] => 1
                                            [#size] => 30
                                            [#maxlength] => 255
                                            [#prefix] => 
                                            [#attributes] => Array
                                                (
                                                    [class] => Array
                                                        (
                                                            [0] => locality
                                                        )

                                                    [autocomplete] => address-level2
                                                )

                                            [#tag] => div
                                        )

                                    [administrative_area] => Array
                                        (
                                            [#title] => County
                                            [#required] => 
                                            [#access] => 1
                                            [#empty_value] => 
                                            [#size] => 30
                                            [#maxlength] => 255
                                            [#prefix] => 
                                            [#render_option_value] => 
                                            [#attributes] => Array
                                                (
                                                    [class] => Array
                                                        (
                                                            [0] => state
                                                        )

                                                    [autocomplete] => address-level1
                                                )

                                            [#options] => 
                                            [#tag] => div
                                        )

                                    [postal_code] => Array
                                        (
                                            [#title] => Postcode
                                            [#required] => 1
                                            [#access] => 1
                                            [#size] => 10
                                            [#maxlength] => 255
                                            [#attributes] => Array
                                                (
                                                    [class] => Array
                                                        (
                                                            [0] => postal-code
                                                        )

                                                    [autocomplete] => postal-code
                                                )

                                            [#prefix] =>  
                                            [#tag] => div
                                        )

                                )

                            [country] => Array
                                (
                                    [#title] => Country
                                    [#options] => Array
                                        (
                                            [GB] => United Kingdom
                                        )

                                    [#render_option_value] => 1
                                    [#required] => 1
                                    [#attributes] => Array
                                        (
                                            [class] => Array
                                                (
                                                    [0] => country
                                                )

                                            [autocomplete] => country
                                        )

                                    [#weight] => -500
                                    [#ajax] => Array
                                        (
                                            [callback] => addressfield_standard_widget_refresh
                                            [wrapper] => addressfield-wrapper
                                        )

                                    [#element_validate] => Array
                                        (
                                            [0] => addressfield_standard_country_validate
                                        )

                                    [#limit_validation_errors] => Array
                                        (
                                        )

                                )

                            [#wrapper_id] => addressfield-wrapper
                            [#prefix] => 

                            [#suffix] => 

                            [name_block] => Array
                                (
                                    [#type] => addressfield_container
                                    [#attributes] => Array
                                        (
                                            [class] => Array
                                                (
                                                    [0] => addressfield-container-inline
                                                    [1] => name-block
                                                )

                                        )

                                    [#weight] => -100
                                    [#access] => 1
                                    [first_name] => Array
                                        (
                                            [#title] => First name
                                            [#size] => 30
                                            [#maxlength] => 127
                                            [#required] => 1
                                            [#attributes] => Array
                                                (
                                                    [class] => Array
                                                        (
                                                            [0] => first-name
                                                        )

                                                    [autocomplete] => given-name
                                                )

                                        )

                                    [last_name] => Array
                                        (
                                            [#title] => Last name
                                            [#size] => 30
                                            [#maxlength] => 127
                                            [#required] => 1
                                            [#prefix] =>  
                                            [#attributes] => Array
                                                (
                                                    [class] => Array
                                                        (
                                                            [0] => last-name
                                                        )

                                                    [autocomplete] => family-name
                                                )

                                        )

                                )

I tried these

 $form['commerce_customer_address']['und'][0]['#columns'][11]['first_name'] = "test";
 $form['commerce_customer_address']['und'][0]['#columns']['first_name'] = "test";
 $form['commerce_customer_address']['und'][0]['#columns'][11]['first_name'] = "test";
 $form['commerce_customer_address']['und'][0]['name_block']['first_name']["value"]['#default_value'] ='test';
 $form['commerce_customer_address']['und'][0]['name_block']['first_name']['#default_value'] ='test';
 $form["commerce_customer_address'"]["und"][0]["value"]['#default_value'] ='test';
 $form['commerce_customer_address']['und'][0]['first_name'] = "test";

and other variations but I am not seeing the value populate

For example this

$form['commerce_customer_address']['und'][0]['name_block']['first_name']['#title']='test';

Changes the title of the field to test, but I still can not set a default value to pre-populate the field on page load.

1

1 Answer 1

3
+50

addressfield is not really a regular form. but uses an address object.

now there is a hook to set de default value, called hook_addressfield_default_values_alter, and you can set it like this:

function hook_addressfield_default_values_alter (&$default_values, $context) {
  $default_values['first_name'] = 'deFaulT';
}

see also here and How can I set a default value for the country in an addressfield?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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